Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "None" when it should return "*** *** ***".

It shouldn't do this since giving the string "hey hey hey" as an input returns the correct answer *** *** ***

def censor(text, word):
        text = text.split()
        for i in text:
            if i == word:
                i = "*" * len(word)
                text[text.index(word)] = i
        text = " ".join(text)
        print (text)

censor("what the hell", "hell")



Printing and returning are different concepts


Sorry, my bad... didn't even think about that


I understand all of @rubysurfer45482 's code here, except the following line:

text[text.index(word)] = i

What does this line mean? I think I know what it is doing, but I don't know why.


I used that bit since you can't index a list by a string. text.index(word) gives you back the integer representing the index of the word you're checking and hence the one you want to substitute in the list.


A simple solution

def censor(text, word):
char='*' * len(word)
return text.replace(word,char)


Why does this NOT work??

def censor(text, word):
star = "*" * len(word)
str_list = string.split(text, word)
new_text = star.join(str_list)
return new_text

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


I cant get that to work for me


Make sure you use proper indentation in your function.


can you please explain what these line are doing
text[text.index(word)] = i
text = " ".join(text)


This line raplaces (word) with i in a list

"text = " ".join(text)" - this line will noy work
it should look like "return (" ".join(text))"


thank a lot for helping me


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