Reputation: 11
How do I remove the last punctuation mark at the end of a word only in python. I have tried this but it removes all the punctuation marks. The solution should not remove any other characters that are not punctuation.
word = "".join([i for i in word if i.isalpha()])
Upvotes: 1
Views: 807
Reputation: 125
If you want to remove all special character from string then you can try following code
import re
text = "your string"
text = re.sub(r'[^\w\s]', '', text)
Upvotes: 0
Reputation:
You can just strip the punctuations
import string
words = ['apple.', 'banana;', 'coconut', 'date!']
print([w.strip(string.punctuation) for w in words])
Upvotes: 2