10/15 censor


This is my code. The result is thisisa****, I don't know how to convert it into this is a ****

def censor(text, word):
text = text.split(' ')
result = ''
for words in text:
print words
if words == word:
result += '*' * len(word)
result += words
return result
print censor('this is a test', 'test')


def censor(text, word):
    text = text.split()
    result = ''
    for words in text:
        print words
        if words == word:
            result += '*' * len(word)+" "
            result += words+" "
    return result.rstrip()
print censor('this is a test', 'test')




Thank you! It works! But I don't understand .rstrip() what does this method do?



def censor(text, word):
 text = text.split(' ')
 result = []
 print text
 for words in text:
     print words
     if words == word:
         result.append('*' * len(words))
 return " ".join(result)

print censor('this is a test', 'test')


each time a word was appended to result a space was also a added at the end.This implies that the final value of result will have a space at the end.
That space must be removed and we can do so with the string.rstrip() method.
The rstrip(right strip) will remove all spaces at the right end of the desire string


Thanks! Making result a list is better than a str


not work for me :frowning:

def censor(text,word):    
tex= text.split()
for t in tex:
    if t==word:            
return " ".join(new_text)

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



I am using this code..

def censor(text,word):
for i in my_string :
my_string = str(my_string)
if word in text :
new_str = string.replace(my_string , word, "*" *\
return " ".join(new_str)

    else :
        return my_string

Error : It returns "[ ' * * * ' , ' * * * ' , ' * * * ' ]" when it should return "*** *** ***"

please help me in rectifying this error..


My solution:

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


Hi, I think my code works fine, but still I'm not allowed to continue :<

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

for i in range(len(split)):
    if split[i] == word:
        newword = newword + " " + "*" * len(split[i])

        newword = newword + " " + split[i]
return newword

print censor("hey hey hey", "hey")


This is my code.

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


I used similar code that everyone here was using, but was having a hard time with the error of having a space at the end of my returned string, and the code itself looking a little convoluted. Here's what I did today:

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


I didn't quite use the same method as what I've found above but I know I am just a little off with my code. Can anyone see what's up?
def censor(text, word):
numast= 0
ast= numast*'*'
string= text.split()
for i in word:
for word in string:
string= string.replace(word, ast)
return cens


This bit of code seems to get the intended result. However it shows up as not correct. Can someone please explain to me if this is oversimplified or why this is not correct. Just have to modify the print text line with an indent to recreate.

def censor(text,word):
word = str(word)
letters = len(word)
asterisks = letters * "*"
if word in text:
--->print text.replace(word,asterisks)
return text
print text

censor("hey hey hey","hey")


This seemed to do the trick. It doesnt include any join. i feel like its lazy but it seems to work regardless of the inputs.

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

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


Man I like to make mine complicated. I try to keep it as simple as I can but I basically forgot the key .append() method. Creating a separate empty list and just appending is nice a straightforward. In my attempt, I ended up finding the index, removing the index, and then re-inserting the new value. Took a while. :slightly_smiling:

def censor(text, word):
    tS = text.split()
    for i in tS:
        loc = tS.index(i)
        if i == word:
            i = '*' * len(word)
            tS.insert(loc, i)
    return ' '.join(tS)
print censor('hey there man i hey you say', 'hey')


Hi, I do it without "split" and "join". The code academy considers its correct.

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


This a very clever solution! Props to you! :slightly_smiling:


just returning this line an getting rid of everything else will do the trick