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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.