这是我目前的代码:
UserSentence = input('Enter your chosen sentence: ')
UserSentence = UserSentence.split()
print(UserSentence)
说UserSentence
是'life is short, stunt it'
,我如何删除.split()
之后的逗号?如果可能的话。
答案 0 :(得分:3)
替换:
In [4]: 'life is short, stunt it'.replace(',',' ').split()
Out[4]: ['life', 'is', 'short', 'stunt', 'it']
如果您要删除所有标点符号,可以使用str.translate
将任何标点符号替换为空格,然后拆分:
s = 'life is short, stunt it!!?'
from string import punctuation
tbl = str.maketrans({ord(ch):" " for ch in punctuation})
print(s.translate(tbl).split())
输出:
['life', 'is', 'short', 'stunt', 'it']