簡體   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