How to Efficiently Remove Punctuations from a String

How to Efficiently Remove Punctuations from a String

8 Different Ways to Clean Strings in Python. Recently I found myself spending many hours trying to make sense of messy text data, and decided to review some of the preprocessing involved. There are many different ways to achieve a simple cleaning step. Today, I will review a couple of different methods to remove punctuations from a string and compare their performances.

Recently I found myself spending many hours trying to make sense of messy text data, and decided to review some of the preprocessing involved. There are many different ways to achieve a simple cleaning step. Today, I will review a couple of different methods to remove punctuations from a string and compare their performances.

Using Translate

The string translate method is a convenient way to change multiple characters to different values at once. *Translate *requires a table that will work as a dictionary to map the strings. The *maketrans *does that job for you.

The maketrans syntax works like str.maketrans('abcd', '0123', 'xyz'). It will create a table that tells *translate *to change all a _with 0, _b _with 1, _c _with 2, etc., and remove _x, y, and z.

Full syntax to remove punctuations and digits using translate is as below.

## importing a string of punctuation and digits to remove
import string
exclist = string.punctuation + string.digits
## remove punctuations and digits from oldtext
table_ = str.maketrans('', '', exclist)
newtext = oldtext.translate(table_)

This approach will entirely remove any character that is in string.punctuation **andstring.digits. *That includes _!”#$%&\’()+,-./:;<=>[email protected][\]^_`{|}~’ _and all numbers.

Using Translate + Join

But sometimes, we might want to add a space in place of these special characters instead of getting rid of them entirely. We can do so by telling a table to change special characters to space instead of excluding them.

table_ = str.maketrans(exclist, ' '*len(exclist))

Additionally, we can simply split and join to make sure this operation does not result in multiple spaces between words.

newtext = ' '.join(oldtext.translate(table_).split())

Using Join + String

We can also just use join instead of *translate, *taking the same exclusion list from the string package we made above.

## using exclist from above
newtext = ''.join(x for x in oldtext if x not in exclist)

Using Join + isalpha

We can forego the exclusion list and just use the string method to call only the alphabets.

newtext = ''.join(x for x in oldtext if x.isalpha())

This approach will only keep the alphabet. As a result, it will also eliminate space between words.

data-science python tutorial nlp

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Data Science With Python Training | Python Data Science Course | Intellipaat

🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...

Python for Data Science | Data Science With Python | Python Data Science Tutorial

🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...

Applied Data Science with Python Certification Training Course -IgmGuru

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

What Are The Advantages and Disadvantages of Data Science?

Online Data Science Training in Noida at CETPA, best institute in India for Data Science Online Course and Certification. Call now at 9911417779 to avail 50% discount.

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.