def censor(text, word):
     for word in text:
         text = text.replace(word,"*")
     return text`

It nearly functions, but my code also replaces the space between the words with stars. What can I do against it?


I prepared the code with some print’s

def censor(text, word):
    print("word as argument: %s" % word )
    for word in text:
        print("word in FOR-IN : {0}".format(word) );
        print("text before replace: {0}".format(text) );
        text = text.replace(word,"*")
        print("text before replace: {0}".format(text) );
    print("=== FOR in loop ended ====")
    return text
print censor("this is text this","this")

I think you are missing an IF statement
as you are replacing all characters 1 by 1

So you also have to think of a way to split the text in words…

nice to know

You could test in labs.codecademy.com
choose Python
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console


i am afraid i am going to have to disappoint you, this code isn’t nearly finished, lets say i do this:

print censor("this hack is wack hack", "hack")

everything (all words, all spaces) are replaces by *, this is because your for loop will loop over each letter:

for word in text:
         print word

Also, text is returned, but it gets never altered inside the for loop. Here is what i would do, use split() to split the strings into an array, then loop over the words in the array, censor a word if this is required (use a if statement to check), join the array into a string (using join()), and voila


So you may want to instead replace the “*” with a value that explicitly represents “word” in asterisks.

n = “*” * len(word)