Censor how to remove the empty


#1

def censor(text,word):
    spl = text.split()
    lst = ""
    for txt in spl:
        if txt == word:
            txt = len(txt) * "*" + " "
            lst = lst + txt
        else:
            lst = lst + txt 
   
    return lst


#2

You need to provide a space after words which are not censored ,
in else part, when you do that you will have to strip up final space from text,
You can simply get rid of the space using string slicing.


#3

def censor(text,word):
    spl = text.split()
    lst = " "
    for txt in spl:
        if txt == word:
            txt = len(txt) * "*" + " "
            lst = lst + txt
        else:
            lst = lst + txt 
    lst = lst[0:len(lst)-1]
    return lst

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

haha i am crazy .


#4

In this line as mentioned, we need to introduce a space using " " ?


#5

def censor(text,word):
    spl = text.split()
    lst = ""
    for txt in spl:
        if txt == word:
            txt = len(txt) * "*" + " "
            lst = lst + txt
        else:
            lst = lst + txt + " "
    lst = lst[0:len(lst)-1]
    
    return lst

it's OK !


#6

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