Throwing problem despite giving accurate answer in censor


I don't know what is wrong in my code. It is giving the absolute answer but compiler throwing up error which is very vague.

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


you have an additional space at the end of your sentence, lets replace the spaces with underscores so we can see it:


the easiest thing to do would simply to slice the last character of using string slicing.

the better thing to do would be to make text a list, then append to text and use .join() in the end to join the list into a string

Strings are immutable in python where as list are mutable, so your solution requires more memory


Thanks for your sound and clear explanation. :slight_smile: