我知道我可以使用.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
我知道我可以编写一个while循环来实现这一点,但我想知道是否有一种更优雅/高效的方法。在
您只需使用^{} :
re.sub(r'[,;\.\!]+$', '', 'hello. world!!!')
相关问题 更多 >
编程相关推荐