Skip to content
Related Articles

Related Articles

Improve Article

Censor bad words in Python using better-profanity

  • Last Updated : 24 Jan, 2021

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:

  1. Import package (profanity)
  2. Declare or input the text to be censored.
  3. Use profanity.censor() method and get the censored text.
  4. 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?

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :