如何从字符串/列表

时间:2015-11-30 11:52:21

标签: python python-3.x

这是我目前的代码:

UserSentence = input('Enter your chosen sentence: ')
UserSentence = UserSentence.split()
print(UserSentence)

UserSentence'life is short, stunt it',我如何删除.split()之后的逗号?如果可能的话。

1 个答案:

答案 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']
相关问题