I didnt get why it is overriding output everytime.I tried with different options with no luck. Can you guide?

def censor(text, word):
    for n in range(len(text_new)):
        if text_new[n] == word:
            #text2 +=text1
            #text2 =text2.append(str(text1))
    return text2


what approach are you going for?

looking at this:

def censor(text, word):
    for n in range(len(text_new)):
        if text_new[n] == word:

i would suggest the simplest approach is to update/re-assign items in your list. simple update the item in the list with asterisks:

text_new[n] = # update list item with asterisks

after the loop, use .join() to convert to string, return the string


My approach was

  • split the text
    *Writing a for loop for each word in text and an IF to compare word in text=word given
    replace with *
  • and join
  • else Join the word as it is.

But Iost in between,blank now. Can you suggest where am i missing?


you have a wrong idea of what join does

join joins a list into a string, you do this in the end (after the loop)

in the loop, if a word requires censoring update the list by inserting asterisks


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.