How to remove all punctuation from text in python

This recipe helps you remove all punctuation from text in python

Recipe Objective

In the world of Content, there are many things that are present like, text, characters, special characters, and many more. Sometimes we just need the text for our ease of access and don't need any special characters or punctuation in it.So, We are going to see how to remove Punctuations from a text and only text will remain.

Master the Art of Data Cleaning in Machine Learning

Step 1- Taking a simple string or text and printing it

simple_text = "It, is better for waking up early in morning !!, than working late nights ;" print("Printing the Simple Text for our Understanding :", simple_text)

Printing the Simple Text for our Understanding : It, is better for waking up early in morning !!, than working late nights;

So from the above we can see that in simple text punctuations are there and we need to remove them. So lets see how to do it.

Step 2 - Storing all punctuations in a Variable

All_punct = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

So in this we are taking a variable named All_punct which consist of all the Punctuations that we want to remove.

Step 3 - Removing punctuations from the text

for elements in simple_text: if elements in All_punct: simple_text = simple_text.replace(elements, "") print("Now let us see the text after removing the punctuations :", simple_text)

Now let us see the text after removing the punctuations : It is better for waking up early in morning than working late nights

Here we can see that the punctuations were present in the text are removed by using the for loop and only text is remaining without any special character or punctuation.

Step 4 - Removing punctuations by using re, importing re

import re

It is more simpler than the other method we used for removing punctuation, just need to import re which is nothing but a regex.

Step 5 - Taking another text and printing it

second_text = "why can't i live freely ?? , It's just the : way i want it, no more interference required !! by any other side ;" print("Printing the original text with punctuations :", second_text)

Printing the original text with punctuations : why can't i live freely ?? , It's just the : way i want it, no more interference required !! by any other side ;

Step 6 - Removing punctuations using re, printing updated one

remove = re.sub(r'[^\w\s]', '', second_text) print("updated text with no punctuations :", remove)

updated text with no punctuations : why cant i live freely Its just the way i want it no more interference required by any other side

So here, we can get a idea about how regex works for removing the punctuations from a text

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.