Python: Censoring a word in text but last word does not censor
Python: Censoring a word in text but last word does not censor
Its probably including the full stop in the last token, so it is comparing dirty.
with dirty
.
The last occurrence of dirty is dirty.
instead of dirty
.
It might be easier to use the replace
function:
def censor(text,word):
return text.replace(word, len(word)**)
Without built-in functions:
def censor(text,word):
while 1:
wordPosition = text.find(word)
if wordPosition < 0:
break
text = text[:wordPosition] + len(word)** + text[wordPosition+len(word):]
return text
Python: Censoring a word in text but last word does not censor
Christopher is correct that its comparing dirty
to dirty.
with a period. As you said you cannot use replace
function, so you could change your if
statement to be
if x.startswith(word) == True: