10. censor



The error mesage is:

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

The error message does not make any sense at all. What have I done wrong. Please help me.

def censor(text, word):
    lst = text.split(" ")
    empty_str = ""
    for i in lst:
        if i == word:
            empty_str += len(i) * "*" + " "
            empty_str += i + " "
    return empty_str


An additional space is placed at the very end is the problem:


i replaced the spaces with a underscore. Now, i hope you see the problem, you need help fixing it, or do you want to try it yourself first, now you know what the problem is?


I think I got it, I'll come back if I have any problems
Actually nevermind I need help


Okay, i would simple check if the last character in your string is a space just before you return it:

if empty_str[-1] == " ":

if it is the case, slice it off:

empty_str = empty_str[:-1]

list slicing is already covered right?


Yes, I've covered list slicing and thanks I got past it now.