[solved]censor


#1

NOT WORKING

def censor(text,word):
new_string = " "
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in text:
if i == word:
new_string_array.append(m)
else:
new_string_array.append(i)

for w in new_string_array:
    new_string += " " + w
return new_string

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


Censor
#2

here:

for w in new_string_array:
    new_string += " " + w

why not simply use .join() like you did before?


#3

The error is due to " " (extra space) as at every iteration it adds before any word!
You can use join() method!
hint**
If you want to solve with this approach then lstrip() method can help!
You can call it at your returning string!


#4

Done :slight_smile: thanks a lot

def censor(text,word):
new_string = " "
new_string_array = []
text = text.split()
m = "*" * len(word)
for i in text:
if i == word:
new_string_array.append(m)
else:
new_string_array.append(i)

return " ".join(new_string_array)
print censor("hey hey hey","hey")

#5