I am trying to build a simple function to censor a specific word in a sentence. For example "hello hello hi"
to "***** ***** hi"
if I feed censor("hello hello hi", "hello")
. Assuming I will not receive punctuation and sentences with upper case letters or empty strings.
After researching online I understand there is simpler solution, for example:
def censor(text, word):
return text.replace(word, "*" * len(word))
I still want to understand from a learning perspective, what I did wrong in the below more complicated code.
def censor(text,word):
split_text = text.split()
length = len(word)
for item in split_text:
if item == word:
item = ("*" * length)
else:
item = item
return " ".join(split_text)