How to improve my 'censor' function?


I want my program to be able to recognise whole words from a combination of letters in my program. Below is my code:

text = raw_input("Please enter text: ")
word = raw_input("Word to be censored: ")
text = text.lower()
word = word.lower()

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

This works fine, for example, entering My name is Robbie in the text prompt, and Robbie in the word prompt, will print: My name is ******.

However, using two words that contain the same letter combination and order produces an odd result. For instance, using Country count and the word as count, it will output this: *****ry *****. Is there a way I can stop this from happening?

Also, I would like to know if it is possible to have more than one word censored, but still using the raw_input technique. I'd prefer it this way as it allows the user to choose what is censored, as opposed to having to modify the arguments in the function when it is called.

Thanks :smile:


Yes, but you are going to have to put some work in.

Regular Expressions.

Good luck, it is just a mini-language!