如何用除字母以外的任何字符分割字符串? 换句话说,我只想要文本中的单词,不需要其他任何东西。
s="(This# is an5example!)"
what_i_want=['This', 'is', 'an', 'example']
更多字符更多字符更多字符更多字符更多字符更多字符
您可以迭代字符串中的字符并测试它们是否是字母字符。添加一些逻辑,这样您就不会在结果列表中放入空字符串,然后就完成了:
s = "(This# is an5example!)"
word = ""
word_list = []
for character in s:
if character.isalpha():
word += character
elif len(word) > 0:
word_list.append(word)
word = ""
print(word_list)
输出
['This', 'is', 'an', 'example']