FAQ: Learn Python - Practice Makes Perfect - censor

This community-built FAQ covers the “censor” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise censor:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

1 Like

A post was split to a new topic: Censor

2 posts were split to a new topic: Why am I Getting an Error?

5 posts were split to a new topic: Can Someone Explain this Code?

A post was split to a new topic: Solving anti-vowel using pop

def censor(text, word):
    words = text.split()
    result = ''
    stars = '*' * len(word)
    count = 0
    for i in words:
        if i == word:
            words[count] = stars
        count += 1

I just so confused with this part of codes

count = 0
    for i in words:
        if i == word:
            words[count] = stars
        count += 1
    result =' '.join(words)


can someone explains it to me?

thanks

have you checked out all the splitted topics like this one:

Can Someone Explain this Code?

2 posts were split to a new topic: Looping over a string

Hello,

The bit of code I wrote passed the test but I don’t think it’s the correct way to do it given what I saw mentioned about the list not being updated if only a replacement is used.

Would someone be able to tell me if there’s anything inherently wrong with this:

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

Thanks!

i don’t think .replace() gives the desired behavior:

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

using replace you get:

th** hack ** a wack hack

i don’t think this should be partial censored.

But there are no test cases within the lesson covering this. So you could argue about this

I understand what you mean now, thanks for your quick response.

that you could argue about, i think the correct output should be:

this hack ** a wack hack

vs:

th** hack ** a wack hack

what your code gives.

but there are no clear specification in this regard. But if it was for censoring like on this forum, hello would be censored to ■■■■ o, not really nice.

Got it, thank a lot!