Need censor help


#1

def censor(text,word):
text=text.split()
new=''
for i in text:
if i==word:
new+=""len(word) + " "
else:
new+=i + " "

return new

Problem is that with this code, getting an extra space for input "Hey Hey Hey" as "*** *** *** " instead of "*** *** ***".How to remove it without using .join() and if using .join() how will the code look like?


#2

The input is a sentence, which split() turns into a list object. Rather than try to construct a sentence manually (where you insert a space), build a temporary list to receive the alternating values.

result = []

result.append(len(word) * "*")

result.append(i)

And at the end,

return " ".join(result)