[Censor] I don't know what to do next


#1



So i have my code like this. In my logical that's the split word is going right, but i find a problem how to replace the split word that has changed into the text :neutral_face:


def censor(text,word):
    kalimat = text
    kata = word
    hasil = ''
    kalimat.split(kata)
    for x in kata:
        kata = kata.replace(x, '*')
        
    hasil = kalimat.split() + kata
    return ''.join(hasil)
    
print censor("what the heck", "heck")


#2

Hi @balbal04,

The spit method does not alter the object on which it is called. Therefore, to work with the list that results from calling split, you need to save a reference to that list. Instead of this ...

    kalimat = text
    kata = word
    hasil = ''
    kalimat.split(kata)

... you should do this ...

    kata = text.split()

Thereafter, kata refers to the list that results from having called the split method.

Instead of this ...

    kata = kata.replace(x, '*')

... create a new empty list prior to the loop. Within the loop, append either x or '*' * len(word) to the new list, depending upon whether or not x matches word.

After the loop call the join method from ' ' rather than from ''.


#3

thanks for detail answer ;D

it's all clear for me now


#4