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"
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.
 
    
    How about this (the drawback is unable to remain the hyphen -),
import string
s = "the cow's milk is a-okay!"
table = string.maketrans(" ", "-") 
new_s = s.translate(table, string.punctuation)  
print(new_s)
# the-cows-milk-is-aokay
 
    
    >>> string = "the cow's milk is a-okay!"
>>> string
"the cow's milk is a-okay!"
>>> string = string.replace(' ','-')
>>> for char in string:
...     if char in "?.!:;/'":
...             string = string.replace(char,'')
...
>>> string
'the-cows-milk-is-a-okay'
 
    
    A simple generator expression could be used to accomplish this:
>>> s = "the cow's milk is a-okay!"
>>> ''.join(('-' if el == " " else el if el not in "?.!:;/'" else "" for el in s))
'the-cows-milk-is-a-okay'
>>> 
