Replacing All Punctuation with Spaces

31 views Asked by At

I need to take a string, any string, and replace all punctuation (',.!", etc.) with a space.

So, if my string is originally

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

the output needs to be

"sally sells seashells    but she s afraid of water"

We were told to convert s into a list, and use a for-loop including an if-block to do this.

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

There are 0 answers