Censor, am I even close to getting the answer?


What can I say guys, this section has frustrated more than any other has me very depressed for my future in coding. The amount of times I have had to look for the answer, yikes!

Am I even close to getting to where I want to get to with my code here?

def censor(text,word):
    word = string.split()
    text = "*" * 5 
    for letter in word:
       text = text + censor(word)
    return text


The first 2/3 of this section got to me too!

There are a couple of hints I can give:
1. make your "*" multiplied by the len(word)
2. Look at your meanings of word and text. I think you have it backwards after the definition.

You need to split text not word. Then loop through text to find word and change it (as mentioned in #1 above)


In fact I am going to have to re-do most of this section as I cheated on most of the first 2/3 of it. I really need to learn it on my own, but the cheating has ingrained some of the basics I was missing which has really helped with the last section of the "Practice".


Tell me about it, this entire section has single-handedly deflated my ego.


I am feeling the same way. This section has consistently stumped me. Once I see the answer, I can reverse engineer to understand the code, but I just CANNOT begin to come close to figuring it out before then.


I hear this is supposed to be the easy part of the whole journey and as someone who wants to be a full stack developer, I am scared shitless.....


and eventually this is what ended up working for me....

def censor(text,word):
text=text.replace(word,'*' * len(word)) <--- indent here
return text <---- indent here
print censor("You are a douchebag", "douchebag")



def censor(text, word):
    ast = ''
    for i in range(0, len(word)):
        ast += '*'
    rep = text.split()  
    replace = []
    count = 0
    for i in rep:
        if i == word:
        count += 1
        if count < len(rep):
            replace.append(' ')
    string = ''.join(replace)
    return string

censor('this hack is wack hack', 'hack')

Wellll... what you did in 4 lines took me 22. For some reason I could not do this without splitting everything up and using a list as intermediate step... Oh well, it got me some extra practice with loops.


Hi, here is a short version:

def censor(text, word):
    return ("*" * len(word)).join(text.split(word))

print censor("Something is bad at here!","bad at")


here is my try:

def censor(text,word):
text = text.split()

empty_list = list("")

for n in text:
    if n == word:
        empty_list += str("*" * len(word)),
        empty_list += str(n),
empty_list = " ".join(empty_list)

return empty_list

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


It works surprisingly well!
I'm astonished! :astonished:
Can you explain how join works?
How it possible find and replace words then concated it with space?


I am having a lot of trouble here. I can copy paste others working answers but I cannot figure out the reasoning.

I think my issue is only with replacing the specific word with the asterisks.

def censor(text, word):
    # printed lines are just for troubleshooting
    print text
    split = text.split()
    print split
    for n in split:
        if n == word:
            filler = "*" * len(word)
            print filler
    print split
    text = " ".join(split)
    print text
    return text
censor("this one is difficult", "difficult")


This looks like it :

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


Yes, of course :slight_smile:
The text.split(word) searches all copies of the string ("word") given in argument, in the outer string ("text"). No matter that we add letters, numbers, space, or special characters, in the computer's perspective they are just keycodes in the string.
Every split() calls the join, what calls the len(), so every splitted part of the outer string will be replaced with stars. The stars number is determined by the length of "word" argument.
So the order is just like in math, inside->out.
Here is a smarter version, now the censor is not case-sensitive:

def censor(text, word):
    return ("*" * len(word)).join(text.lower().split(word.lower()))

print censor("Something is bad at here!, Bad aT hERe!","bad at")


Hmm, so split is to search and cut? Then, join is to replace and glue them together?
I understand about len method. It's just python method is too awesome, i slowly understand it comprehensively because my previous languages are C, C++, SQL, and PHP.
Currently i'm trying to understand filter.
Sorry for bad english, it's not my native. Thanks before :wink:


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