首页 > 解决方案 > 如何替换字符串中的空格逗号?

问题描述

我有以下字符串,我想将其解析为单词。这是字符串语句

text = "If it is hot , don’t touch"

到目前为止我已经尝试过:

import string

text = "If it is hot , don’t touch"

words = [word.replace(',', '') for word in text.split()]
print(words)

但是我得到了以下结果:

['If', 'it', 'is', 'hot', '', 'don’t', 'touch']

我想要的结果是:

['If', 'it', 'is', 'hot', 'don’t', 'touch']

标签: pythonstring

解决方案


text = "If it is hot , don’t touch"
newtext = text.replace(",", "")
words = newtext.split()
print(words)

推荐阅读