In this article, we will learn how to Censor bad words using Python. For this, we are using the better_profanity module from python.
Installation
pip install better_profanity
For censoring bad words, we are using profanity.censor() method from better_profanity. So, we discuss first its syntax and arguments.
Syntax: profanity.censor(text, censor_char=’*’)
Parameters:
text : text to be censor
censor_char : ‘*’ by default, character to censor bad words
Return value: censored text
Approach:
- Import package (profanity)
- Declare or input the text to be censored.
- Use profanity.censor() method and get the censored text.
- Print censored text.
Example 1:
Python3
# importing package from better_profanity import profanity # text to be censored text = "What the shit are you doing?" # do censoring censored = profanity.censor(text) # view output print (censored) |
Output:
What the **** are you doing?
Example 2:
Python3
# importing package from better_profanity import profanity # text to be censored text = "What the shit are you doing?" # do censoring censored = profanity.censor(text, '$' ) # view output print (censored) |
Output:
What the $$$$ are you doing?