用空格替换所有标点符号

时间:2017-09-01 22:30:29

标签: python python-3.x punctuation

我需要带一个字符串,任何字符串,并用 空格 替换所有标点符号(',.!"等)。

所以,如果我的字符串最初是

"sally sells seashells... but she's afraid of water"

输出需要

"sally sells seashells    but she s afraid of water"

我们被告知要将s转换为列表,并使用包含if-block的for循环来执行此操作。

s = "She sells seashells... but at home she's afraid of water."
alph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def removePunctuation(s):
    slist = list(s)
    for i in slist:
        if i not in alph:
            slist.replace(i, ' ')

0 个答案:

没有答案