I need to convert my string so that it is made into a special format
ex:
string = "the cow's milk is a-okay!"
converted string = "the-cows-milk-is-a-okay"
import re
s = "the cow's milk is a-okay!"
s = re.sub(r'[^a-zA-Z\s-]+', '', s) # remove anything that isn't a letter, space, or hyphen
s = re.sub(r'\s+', '-', s) # replace all spaces with hyphens
print(s) # the-cows-milk-is-a-okay
Here a 'space' is any whitespace character including tabs and newlines. To change this, replace \s with the actual space character .
Runs of multiple spaces will be replaced by a single hyphen. To change this, remove the + in the second call to re.sub.
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