In this shot, we will learn about the remove_punct()
method in Python.
The remove_punct()
method is an in-built function that is provided by the clean-text
library in Python.
We can use it to clean our data, which has punctuations in it.
We need to install the clean-text
from pip
to use it in our programs:
pip install clean-text
from cleantext import clean
clean(text, no_punct=True, replace_with_punct="$")
Where:
clean
is the function that is provided by the cleantext
.no_punct
to True
. Then, it will call the in-built remove_punct()
function.replace_with_punct
. Here, we will pass $
to replace the punctuations in the given text.Let’s look at an example.
#import the clean functionfrom cleantext import cleantext = "This, is a sample text! which [has) punctuations."#display the text after removing the punctuationsprint(clean(text, no_punct=True))
clean
function, which is provided by the cleantext
module.text
with the clean
function.We can replace the punctuations with a string, instead of removing them. We will replace the punctuations with the character $
in the following code. We will do this by passing a character or string to the replace_with_punct
parameter:
#import the clean functionfrom cleantext import cleantext = "This, is a sample text! which [has) punctuations."#display the text after removing the punctuationsprint(clean(text, no_punct=True, replace_with_punct="$"))