python - Converting a string, removing special characters and replacing -


i need convert string made special format

ex:

string = "the cow's milk a-okay!"  converted string = "the-cows-milk-is-a-okay" 

import re  s = "the cow's milk a-okay!" s = re.sub(r'[^a-za-z\s-]+', '', s)  # remove isn't letter, space, or hyphen s = re.sub(r'\s+', '-', s)  # replace spaces hyphens print(s)   # the-cows-milk-is-a-okay 

here 'space' whitespace character including tabs , newlines. change this, replace \s actual space character .

runs of multiple spaces replaced single hyphen. change this, remove + in second call re.sub.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -