Open In App

Censor bad words in Python using better-profanity

Last Updated : 24 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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?

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads