1

I'm trying to get this function to return "****** * * *********" (not a finished product obviously, just trying to figure out how it works step by step), but this code just returns the text without replacing anything. The way I see it, it should be checking every value in the string "Text" and then for every value, replace that value, i, with an asterisk. Where am I going wrong here?

def censor(text, word):
    for i in text:
        text.replace(i, '*')
    return text
print censor("Hello, you are beautiful", "beautiful")
martineau
  • 112,593
  • 23
  • 157
  • 280
Cdhippen
  • 551
  • 1
  • 9
  • 27

1 Answers1

1

You want to replace the entire word, and then you want to replace this with as many * as there are letters of the word you're replacing:

def censor(text, word):
    return text.replace(word, '*' * len(word))

print censor("Hello, you are beautiful", "beautiful")

# returns: Hello, you are *********
Robert Seaman
  • 2,234
  • 14
  • 18