Censor exercise Python 2 course

Can anyone tell me why this code doesn’t work on the Python2 course “Censor” exercise? (https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/censor-)

def censor(text, word):
ast = “”
for letters in word:
ast += “*”
text.replace(word, ast)
return text

print censor(“hello world”, “world”)

1 Like

Hey there @bytesurfer32871 :grinning:

In th future could you please format your code when you post, it saves indentation and makes it much easier to use.
Simply click this button:

And then put your code here:


I believe your problem is in your use of .replace(). It does not modify a string, it returns a copy of the string which is modified;

word = "this"
new_word = word.replace("this", "that")

print word          #prints this since word was never modified
print new_word      #prints that since it is a modified copy of word

2 Likes

Thanks for the help and the formatting for code tip!

2 Likes