Censor


#1

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

def censor(text, word):
    
    text_new=text.split()
    
    text2=''
    for n in range(len(text_new)):
        if text_new[n] == word:
            text1=text_new[n].replace(text_new[n],'*'*len(word))
            #text2 +=text1
            #text2 =text2.append(str(text1))
            text2="".join(text1)
            
            
        else:
            text2="".join(text_new[n])
           
            #text2+=str(text_new[n]
           #text2=text2.append(str(text_new[n]))
    #text2="".join(text2)       
    return text2

#2

what approach are you going for?

looking at this:

def censor(text, word):
    text_new=text.split()
    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


#3

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?


#4

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


#5

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