remove punctuation and special characters from string python code example
Example 1: remove punctuation from string python
#with re
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
#without re
s = "string. With. Punctuation?"
s.translate(str.maketrans('', '', string.punctuation))
Example 2: remove special characters from string python
>>> string = "Special $#! characters spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'