How to remove all punctuation from text?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to remove all punctuation from text?

How to remove all punctuation from text?

This recipe helps you remove all punctuation from text

0

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.

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

Relevant Projects

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

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.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.