Jan-14-2021, 12:10 PM
hello,
I am trying to write a case insensitive censor function which takes an input(text, banned) with banned being a list of banned words to censor from the text. I have the basic censor function written however I'm having trouble with trying to make it case insensitive. I'll put my code below.
I am trying to write a case insensitive censor function which takes an input(text, banned) with banned being a list of banned words to censor from the text. I have the basic censor function written however I'm having trouble with trying to make it case insensitive. I'll put my code below.
def censor2(text, banned): for i in banned: if i in text: text = text.replace(i,'*'*len(i)) return textThis function works for the most part, except when the banned word has different cases in the the text. Note that if the input is (Hello World, hello), I cannot simply insert the line text = text.lower() before the for loop as this would return ***** world, instead of ***** World. Any advice anyone has about making this function case insensitive without lowering all of the text as the output I'd really appreciate!