Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the program to get a line of text as input from the user and print that text with all of the "curse words" replaced by ****

If there are no "curse words" in the text entered by the user, the text is returned, unchanged.

Once the program has censored the text, the user must be asked to enter more text or just hit the [Enter] key to quit.

What I have tried:

Python
userText = input("Enter some text to be censored: ")
Posted
Updated 28-Jan-22 5:31am
v2
Comments
[no name] 28-Jan-22 0:01am    
https://stackoverflow.com/questions/12538160/replacing-specific-words-in-a-string-python
Richard Deeming 28-Jan-22 5:44am    
Be careful you don't make the clbuttic[^] mistake, or you'll end up with the Scunthorpe problem[^]. :)

1 solution

You can achive this through using join(), split() + list of censors words. You code like be

userText = input("Enter some Text to be Censored: ")
# Initialization of censors words list and replaced by
censors_words = ['dang', 'darn', 'freak']
repl_word = '****'
result = ' '.join([repl_word if idx in censors_words else idx for idx in userText.split()])
print("Result : " + str(result))

Entered TextL Hello dang for darn and freak
Result : Hello **** for **** and ****
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900