Python Forum
Case Insensitive Censor Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Case Insensitive Censor Function
#1
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.

def censor2(text, banned):
    for i in banned:
        if i in text:
            text = text.replace(i,'*'*len(i))
    return text
This 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!
Reply


Messages In This Thread
Case Insensitive Censor Function - by HNiChuimin - Jan-14-2021, 12:10 PM
RE: Case Insensitive Censor Function - by Larz60+ - Jan-14-2021, 08:38 PM
RE: Case Insensitive Censor Function - by Serafim - Jan-14-2021, 11:13 PM
RE: Case Insensitive Censor Function - by Serafim - Jan-15-2021, 08:20 AM
RE: Case Insensitive Censor Function - by Serafim - Jan-16-2021, 12:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Censor Thread palmtrees 6 7,216 Oct-05-2016, 06:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020