0

As of now it takes the punctuation out, but I cannot figure out how to make it add a space instead of just taking out the punctuation. Currently I have:

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

It's in a interactive textbook so it will automatically do the tests on their site.

John doe
  • 1
  • 2

1 Answers1

1

By adding an else to your code:

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
Foxan Ng
  • 6,883
  • 4
  • 34
  • 41