Practice Makes Perfect censor

def csor(text, word): l = len(word) for i in range(len(text) - len(word)): idx = 0 a = True if text[i].lower() == word[0].lower(): for i in range(l): if text[i].lower != word[i].lower(): a = False if a == True: for j in range(i, l): text.replace(text[j: j+l], "*" * l) return text print csor("this hack is wack hack", "hack")

Ques, Write a function called censor that takes two strings, text and word , as input. It should return the text with the word you chose replaced with asterisks.
text.replace doesnt seem to work. Am i using it correctly ?

You’re on the right lines for locating the issue, .replace is a method that returns a new string with the relevant changes performed, it won’t alter the existing value https://docs.python.org/3/library/stdtypes.html#str.replace. Use the result from evaluating that method if that’s what you need.

1 Like

Ok, so i need to split the words and then use replace method and assign the return value to a different variable right ?

1 Like

Also, i just realized that this can be solved with just 2 lines of code

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