10/15 Censor


#1

i do not whats wrong with my code. I tried below code. Please help me

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

and here is the error message
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['', '', '']" when it should return " *** ***".


#2

Try this

def censor(text,word):
    lt=text.split()
    for i in range(0,len(lt)):
        if lt[i]==word:
            lt[i]="*"*len(word) # your code lt[i]=""len(word)

    # took this line out  "".join(lt)     
    return " ".join(lt)
    
print censor("hey hey hey", "hey")

you can also use this

def censor(text, word):
    return text.replace(word, "*"*len(word))
    
print(censor("this hack is wack hack", "hack"))

or you can go by this

def censor(text, word):
    return ("*"*len(word)).join(text.split(word))
    
print(censor("this hack is wack hack", "hack"))

#3

Thank you so much @rydan

sorry for typo, i was using this code but i think i was making mistake with below statement at last line of code.

btw thanks again !


#4
 def censor(text,word):
    text=text.split()
    for i in range(0,len(text)):
         if text[i]==word:
            text[i]= "*" * len(word)
    newtext= " ".join(text)
    return newtext
        
text=raw_input("Enter Text")
word=raw_input("Enter word")
print censor(text,word)

Try this>>>


#5
def censor_text(text,word):
    return text.replace(word, len(word) * '*')

#6

.join(text.split(word))----> I don't understand .. plz.. explain ...


#7

redan: many thanks. I was struggling with string.split & join. I could not get the code to work. The text.replace seems to be the more elegant way to accomplish the task goal. Anyway, thanks much for taking the time to post your answer.


#8

Whoah, you're a wizard! Thank you!


#9

you are genius! Thank you!