Censor - a way to do it without creating a list


#1

def censor(text,word):
words = text.split()
sentence = ""
for x in words:
if x == word:
length = len(word)
x = "*" * int(length)
sentence = sentence + " " + x
else:
sentence = sentence + " " + x
print sentence[1:]
return sentence[1:]

censor("hey hey hey","hey")

did anyone else do it like this?


#2

You actually don't need a bunch of that code, once you get further in you will start to learn about the modules that python has. One in particular is called re or regular expression module. Regexs are a mini language but you can do things like,

from re import sub

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

print(censor('hey', 'hey hey hey, you!'))
# OUTPUT: *** *** ***,you!

#3

looking forward to learning how to do that!