I know I can use
.translate(None, string.punctuation)
However, only strip the final punctuation.
However, only strip the final punctuation
This is sentence one. This is sentence two!
This is sentence one. This is sentence two
This sentence has three exclamation marks!!!
This sentence has three exclamation marks
You could simply use rstrip
:
str.rstrip([chars])
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:
>>> import string
>>> s = 'This sentence has three exclamation marks!!!'
>>> s.rstrip(string.punctuation)
'This sentence has three exclamation marks'
>>> s = 'This is sentence one. This is sentence two!'
>>> s.rstrip(string.punctuation)
'This is sentence one. This is sentence two'
>>> s = 'However, only strip the final punctuation.'
>>> s.rstrip(string.punctuation)
'However, only strip the final punctuation'