#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))
Here is what the above code is Doing:
1. We create a translation table using str.maketrans().
2. We use the translate() method to remove all punctuation from the string.
3. We use the empty string as the first argument to the translate() method.
4. This tells Python to create a mapping of every character in the string to an empty string.
5. We use the string.punctuation constant as the second argument.
6. This tells Python to remove every character that is in the punctuation constant.