Saturday, December 28, 2024
Google search engine
HomeLanguagesCensor bad words in Python using better-profanity

Censor bad words in Python using better-profanity

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?
RELATED ARTICLES

Most Popular

Recent Comments