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
Post a Comment