Need help to replace substring with "*" - censor problem


def censor(text,word):
    split_list = text.split(",")
    #print split_list
    for i in split_list: 
        for j in range(0,len(i)-1):
            if j < len(word)-1:
                if i[j] == word[j]:
    join_list = ",".join(split_list)
    return join_list

print censor("heyheyhey","hey")


Here's a totally efficient way to do it that uses a method called .replace()

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

Let me break it down

text.replace you begin with the first argument of your censor function.

(word, "*" *len(word)) In the parentheses the you type in word first because it is what you want to replace for '*' the asterix.

len(word)) You put this so python only puts asterix that amount to the length of word


Thank you very much...that's pretty short and it worked :slight_smile:
but i need to go with split and join...any help..


You want to use the .split() method is that what you're saying?


yes exactly...the way it is given in the hint...element by element and then using index for each element.
refer my code above and suggest what change i need...


Your code can be shortened. It is not efficient to use two for loops and two if statements.

    def censor (text, word):
#Empty list for storing censored word
        new_text = []
#For loop to go through the text and split it
        for x in text.split():
#If statement to check if the for loop has the correct number of letters in word
            if x == word:
#Replace  the letters for asterix
                x = '*' * len(word)
#add the censored word into the empty list.
#Bring together the letters in` new_text` 
        return ' '.join(new_text)

This code above uses both the .split() and ' '.join() method But it is shorter and easier to read. :slight_smile:


yeah correct this should be the steps...but its not working for such cases


ideally it should return ****** but its returning ananan


The way you call it is the issue.

try call the code above with something like...

censor("I am a programmer", "programmer")


words are generally separated by spaces, so you can just use split without any arguments since the default is spaces:

split_list = text.split()

then you join the string together with spaces (no comma's (,)):

join_list = " ".join(split_list)

words in sentence are normally separated by spaces, it would be annoying read if there was a comma after each word.

and then when you call the function, make sure the words have spaces in them:

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


Yes, that is why this,

Was not working for him the first time, no spaces.


yeah it will work for all strings having some delimiter :slight_smile:

but cases like censor("heyheyhey","hey") it wont work.
for that we have to use the short logic you suggested.

Let me know if you get any solutions for this particular call. thanks :slight_smile:


heyheyhey is now considered to be one word, because there is no delimiter, so in other to censor it you would need:

print censor("heyheyhey","heyheyhey")