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

Naoko Nishimura picture Naoko Nishimura · Apr 17, 2013 · Viewed 15.5k times · Source

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.

Answer

Joran Beasley picture Joran Beasley · Apr 17, 2013
print " ".join(word.rstrip("!") for word in text.split())