10. Censor


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

I don't see a problem

def censor(text, word):
    result = ""
    for each in text.split():
        if each == word:
            result += "*" * len(word) + " "
            result += each + " "
    return result
print censor("hey hey hey","hey")


lets replaced the spaces with underscores so we can see the spaces, what you have:


what you should have:


you can slice off the last space, that would be the easiest solution, you have done string slicing before


result = result[:len(text)]


that will work, or you can use negative numbers to slice of at the right hand sight:


you can even return the slice:

return result[:-1]

so many options


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.