Censor 10/15


Hi there,

I wrote this code as solution for this exercise. It asks for two inputs (two strings) and checks whether the second string can be found in the first string. If it finds it then it replaces the found text in the first string with asterisks. But for some reason the page showed that my code did not pass the test. Does my code seem to work? Should it solve the exercise?

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


Start by considering (and explaining) what is happening differently from what you want
When you share code, make sure that it’s a byte-for-byte copy, if you let the forum apply formatting to it then it’s not the same code any more, defeating the purpose of sharing it.
I suggest avoiding string’s replace method because it is capable of doing the censoring all by itself, it’s like building a house except you already have a house so you don’t need to build it


Hi, ok, I modified my original message, thanks for the reply.


i think ionation covered it nicely:

furthermore, i don’t agree with the way the replace works for censoring:

print censor("this is what i mean", "is")

will give:

th** ** what i mean

this shouldn’t be partial censored.

so don’t use replace().

And you haven’t explained how your code differs from what you expect.

Maybe insert some print statements so you can see what your code is doing?


Hi, thank you. Let me try to cover it then.

This is the exercise:
Write a function called censor that takes two strings, text and word, as input. It should return the text with the word you chose replaced with asterisks.

This is my code:

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

I expect it replaces the word in the text with as many asterisks as long the word is. In contrary to this, I receive the following error message:
“Your function fails on censor(“hey hey hey”,“hey”). It returns “hey hey hey” when it should return " *”."

So it seems my code does not change the text, however it should be and I do not understand why. I inserted print commands to see what the inputs are and what my function returns and it seems the error message is right, my function returns the original input. I am wondering why the “text.replace(word, “*” * len(word))” does not do what it should.

Regarding the explanation, now at least I understand that replace() does the censoring by itself so creating the function is pointless. Still I don’t understand why my code does not work…

Thanks for your reply in advance.


it does, you just do not do anything with the result returned by the replace method


You are right. I thought the "text.replace(word, “*” * len(word)) modifies the text but it seems it does not. I made a variable equal to the result of this statement and now it is okay. Thanks for all the help.

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


But you are ignoring most of our important advice which is not to use replace() at all


I understand your concern, but for me the built in solution is much more difficult than this solution. But I understand the risk of using replace().


Information on how various things behave is typically found in documentation which in turn can be found by googling. https://docs.python.org/3/library/stdtypes.html#str.replace
Things don’t change their behaviour based on what a programmer wants, the programmer has to use the behaviour that is there and make sure everything fits. The programmer therefore needs to know how each thing behaves and will therefore have to look up pretty much everything they use.

Though, when you know what most things do, it’s often possible to figure out how something behaves. For example, strings can’t be changed so therefore string’s replace method must return the result or it would be entirely useless.
Other times you’re the one defining that behaviour and should therefore already know how it behaves, or at least be able to find and look at your own code to figure it out.

If you ever don’t know what something in your code does, then it’s a bug. Why would it work if the author doesn’t know what they wrote? It won’t. It might do something, but that’s by accident. Gotta be able to argue for what everything does and if that’s not possible then it’s time to do a bit of learning.

So when you’re writing something it usually goes like:

  • Figure out what actions you need
  • Figure out how to do each action individually by googling/testing
  • Fit them together so that they produce the desired outcome

No guessing, always find out more when you don’t know enough.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.