How to remove all punctuation marks following a line?

This is for a game in which the user can enter a value such as “Iced tea ..” I would like to manipulate the string to return “Iced tea” without any punctuation marks.

Look for the most elegant / simplest solution for python.

I tried

def last_character(word): if word.endswith('.' or ','): word = word[:-1] return word 

which works if there is only one punctuation mark at the end. But this is not all-encompassing.

Java solution found :

 String resultString = subjectString.replaceAll("([az]+)[?:!.,;]*", "$1"); 
+6
source share
3 answers
 >>> 'words!?.,;:'.rstrip('?:!.,;') 'words' 
+15
source

In this case, rstrip is probably what you want. But overall, it’s not difficult to translate something like this Java into Python. A direct translation will look like this:

 import re subject_string = 'Iced tea..' result_string = re.sub('([az]+)[?:!.,;]*',r'\1',subject_string) 
0
source
 import string s='aaa...,' count= 0 >>> for l in s[::-1]: ... if l in string.punctuation: ... count = count +1 ... else: ... break >>> s[:c] 'aaa' 
0
source

Source: https://habr.com/ru/post/914771/


All Articles