繁体   English   中英

将标点符号更改为空格(空格字符)

[英]Changing Punctuation to White space(space character)

到目前为止,它已经删除了标点符号,但是我不知道如何使它添加一个空格,而不是仅仅删除标点符号。 目前我有:

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

它位于交互式教科书中,因此它将自动在其站点上进行测试。

通过在代码中添加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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM