1

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, ' ')
ColeFemo
  • 19
  • 2
  • Basically, `str.replace()` function returns the string with the new characters replaced. You'll need to keep this in mind, and perhaps make a new string of the new characters – TerryA Sep 01 '17 at 22:32
  • You should post a [mcve] as correctly-formatted text, **not** screenshots. @TerryA also they're trying to call it on a list... – jonrsharpe Sep 01 '17 at 22:33
  • @jonrsharpe Oh, I didn't notice that. Technically the logic is still the same though – TerryA Sep 01 '17 at 22:39
  • @jonrsharpe There you go. Sorry, first time on this website. – ColeFemo Sep 01 '17 at 22:40
  • Note you should also include *what happens*, presumably an `AttributeError` traceback. – jonrsharpe Sep 01 '17 at 22:41

0 Answers0