Censor removing spaces


#1



Practice Makes Perfect: Censor


I've gotten it to replace the words with asterisks, but when I split the string it is removing the spaces between words and I can't figure out how to get them back in:

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".


def censor(text, word):
    text=text.split()
    length=len(word)
    new_text=""
    for x in text:
        if x==word:
            x="*"*length
            new_text+=x
        else:
            new_text+=x
    return new_text
print censor("hey hey hey","hey")


#2

you could add the spaces:

new_text= new_text + x + " "

this will give an additional space at the end of the sentence, you will have to slice it off

or change new_text to a list, use append() and in the end .join()


#3

That fixed it! Thank you!


#5