10. Censor


I am getting the error message:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".

The code is supposed to censor a word and replace it with asterisks. I'm fairly new to Python, and I am not sure what I did wrong.

def censor(text, word):
    word = word.split()
    final = []
    for i in word:
        if i == word:
            final += "*" * len(i)
            return text.join(final)
            return text
print censor("hey hey hey", "hey")


Hi, @geckosrock ,

Here, you have overwritten the function parameter, word, with the result of applying split() to word...

word = word.split()

Instead, you need to divide up text, which contains the words that need to be tested. You can do this as follows, placing the words into a words list ...

words = text.split(" ")

Now, you can iterate through words ...

for i in words:

Inside the loop, you have a test ...

if i == word:

If i and word are equivalent, append the censored word, as asterisks, to the final list ...


... but do not return anything, just yet.

If, on the other hand, i and word are not equivalent, append the actual item to the final list, still not returning anything ...


Then, after the loop has completed all its iterations, put the items in the final list back together and return the result ...

return " ".join(final)

Make sure to get the indentation right on the return statement, so that it is not part of the loop.

Let us know how it works out.


Thank you so much!!! I did everything you said, and now the code works perfectly! Thanks for spending the time to figure out the problems! You guys are awesome! :slight_smile:


One quick question: What do the quotation marks do in these two lines?

words = text.split(" ")

return " ".join(final)

Are they maybe adding spaces between the returned asterisks?


" " is a space character.

The first of the two lines you have posted splits up the string that is referred to by text, splitting it wherever there is a space, and assigns the parts to words, as a list. The spaces are not included in the list.

The second one joins together the strings that are in the list referred to by final into a single string, with a space character between each adjacent pair of the joined components.


That makes a lot of sense! Thank you for the explanation. :slight_smile:


so , i found that str.replace() is pretty handy

def censor(text,word):
    result = ""
    if(word in text):
        result = text.replace(word,"*" * len(word))
    return result


Oh wow! I had no idea that function existed! Thanks for the info! :slight_smile:


hey can you tell me what do we do to join the items in list without keeping spaces between them...?


@appylpye already covered this? the character between the quotation marks it what joins is what join() uses to join the string. so for example if you want a hyphen between the words:


but if you would have read appylpye answer, you could have known this already



I often visit the Official Python Web Site to look at their documentation, in order to learn about particular details about methods, and to casually browse for other information that may be useful at a future time.

See the documentation regarding str.join(iterable) for Python 3. It also applies to Python 2, which Codecademy still teaches.

Another good place to go on a regular basis is Stack Overflow. There, users discuss lots of different topics regarding programming, and often probe interesting details. For an example, see Python join, why is it string.join(list) instead of list.join(string)?.


I did it in this way is it better(faster when ran) compared to different logics given here or BAD :slight_smile:

def censor(text,word):
list1=text.split(" ")
for n,x in enumerate(list1):
if x==word:
return " ".join(list1)


yes, picking a good approach will improve the running time. People who are new to programming sometimes struggle with passing the exercise, they are not yet ready to optimize.

even your version could be topped


def censor(text, word):
    words = text.split("")
    final = []
    for i in words:
        if i == word:
    return " ".join(final)

HELLO guys can u help me thx!!!


your split:

words = text.split("")

i would split by spaces (" ")


there is a problem with this solution if for example you pass (There is no world like ours but worldwide economy is even worse, world)
You will get
There is no ***** like ours but *****wide economy is even worse


Yes, it is best to split the text by spaces, then work with the resulting list to censor individual words.