I'm using this to remove spaces and special characters and convert characters to lowercase:
''.join(e for e in artistName if e.isalnum()).lower()
I want to:
replace spaces with -
if the string starts with the word the
, then it
So that, for instance, The beatles music!
would become beatles-music
.
Use the replace() method to replace spaces with dashes in a string, e.g. str. replace(/\s+/g, '-') . The replace method will return a new string, where each space is replaced by a dash.
Use the String. replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.
Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
artistName = artistName.replace(' ', '-').lower()
if artistName.startswith('the-'):
artistName = artistName[4:]
artistName = ''.join(e for e in artistName if e.isalnum() or e == '-')
It sounds like you want to make a machine readable slug. Using a library for this function will save you lots of headache. python-slugify does what you are asking for and a bunch of other things you might not have even thought of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With