How to Remove Punctuation from Strings in Python?

As a data scientist working with text data as part of my project, I frequently encounter strings filled with punctuation marks that need to be cleaned before analysis. I researched more about this topic and found three effective ways to accomplish the task. In this tutorial, I will explain various methods to remove punctuation from strings in Python with suitable examples.

Remove Punctuation from Strings in Python

Let us see some important methods to remove punctuation from string in Python.

Read Convert Int to Bytes in Python

1. Use the translate() Method

One of the most efficient ways to remove punctuation from a string in Python is by using the translate() method along with the maketrans() method [^1^]. Here’s an example:

import string

text = "Hello, my name is John! I'm from New York City."
print(text.translate(str.maketrans('', '', string.punctuation)))

Output:

Hello my name is John Im from New York City

I have executed the above example code and added the screenshot below.

Remove Punctuation from Strings in Python

The maketrans() method creates a translation table that maps each punctuation character to None, effectively removing them. The translate() method then applies this table to the string, stripping away all punctuation marks ^2^.

Check out Convert DateTime to UNIX Timestamp in Python

2. Use a For Loop and Conditional Statement

Another approach is to manually iterate through each character in the Python string using a for loop and only include non-punctuation characters in the resulting string [^3^][^4^]. Here’s how:

import string

def remove_punctuation(text):
    no_punct = ""
    for char in text:
        if char not in string.punctuation:
            no_punct += char
    return no_punct

text = 'Hi there! My name is Sarah, and I live in Los Angeles, CA.'
print(remove_punctuation(text))

Output:

Hi there My name is Sarah and I live in Los Angeles CA

I have executed the above example code and added the screenshot below.

How to Remove Punctuation from Strings in Python

While this method works, it is less efficient compared to using translate(), especially for larger strings, since it requires iterating through each character one by one.

Read Concatenate String and Float in Python

3. Use Regular Expressions

Python Regular expressions provide a flexible way to define and remove punctuation from strings [^5^]. Here’s an example using the re module:

import re

text = "I'm excited to visit Washington, D.C. next week!"
print(re.sub(r'[^\w\s]', '', text))

Output:

Im excited to visit Washington DC next week

I have executed the above example code and added the screenshot below.

Remove Punctuation from Strings in Python re

The regular expression [^\w\s] matches any character that is not a word character (letters, numbers, underscores) or whitespace. The sub() function replaces these matches with an empty string, effectively deleting the punctuation.

Check out How to Find the Largest and Smallest Numbers in Python?

Handle Specific Punctuation Marks

In some cases, you may want to remove only certain punctuation marks while preserving others. You can achieve this by defining your own set of characters to remove [^1^][^5^]:

text = "I bought apples, oranges & bananas at the store for $5.99!"
exclude = set('!@#$%^&*()_-+={}[]|\\:;"<>,.?/')
print(''.join(char for char in text if char not in exclude))

Output:

I bought apples oranges  bananas at the store for 599

Here, we define a set called exclude containing specific punctuation characters to remove. We then use a generator expression inside join() to build the resulting string, skipping any characters found in the exclude set.

Check out How to Check if a Python String Contains a Substring?

Conclusion

In this article, I explained how to remove punctuation from strings in Python. I discussed mainly three methods to achieve this task such as using the translate() method, for loop and conditional statement, and regular expression.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.