Search code examples
pythonpython-2.7strip

How to strip characters from the right side of every word in Python?


Say, if I have a text like

text='a!a b! c!!!'

I want an outcome like this:

text='a!a b c'

So, if the end of each words is '!', I want to get rid of it. If there are multiple '!' in the end of a word, all of them will be eliminated.


Solution

  • print " ".join(word.rstrip("!") for word in text.split())