caroline gwandomba
caroline gwandomba

Reputation: 11

How can I remove punctuation at the end of a word in python?

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

Answers (2)

vkSinha
vkSinha

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

user5386938
user5386938

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

Related Questions