截至目前,标点符号已被删除,但我无法弄清楚如何使其添加空格而不是仅仅取出标点符号。 目前我有:
punctuation = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~"
def remove_punct(theStr):
theStr_sans_punct = ""
for letter in theStr:
if letter not in punctuation:
theStr_sans_punct = theStr_sans_punct + letter
return theStr_sans_punct
它位于交互式教科书中,因此它会自动在其网站上进行测试。
答案 0 :(得分:1)
在您的代码中添加else
:
punctuation = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~"
def remove_punct(theStr):
theStr_sans_punct = ""
for letter in theStr:
if letter not in punctuation:
theStr_sans_punct = theStr_sans_punct + letter
else:
theStr_sans_punct = theStr_sans_punct + " "
return theStr_sans_punct