It's for a game in which the user can input a value like "Iced tea.." I would like to manipulate the string to return "Iced tea" without the trailing punctuation marks.
Looking for most elegant / simplest python solution.
Tried
def last_character(word):
if word.endswith('.' or ','):
word = word[:-1]
return word
which works if there's only one punctuation mark at the end. But it's not all-encompassing.
Found a Java solution:
String resultString = subjectString.replaceAll("([a-z]+)[?:!.,;]*", "$1");
You can use this: Regex. Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");
We can use the JavaScript string replace method with a regex that matches the patterns in a string that we want to replace. So we can use it to remove punctuation by matching the punctuation and replacing them all with empty strings.
Use regex to Strip Punctuation From a String in Python The regex pattern [^\w\s] captures everything which is not a word or whitespace(i.e. the punctuations) and replaces it with an empty string.
>>> 'words!?.,;:'.rstrip('?:!.,;')
'words'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With