Strip Punctuation from a String in Python



When working with the text data in Python, We will come across the situations where we find the unwanted punctuation marks (like .,:?!) to remove.

Removing punctuation helps to normalize text, making it easier to analyze. Python provides the several ways to achieve this, Let's dive into the article to learn ore about it.

Using Python translate() Method

The Python str.translate() method is a sequel for the maketrans() method in the string module. This method uses the translation table generated by the maketrans() and translate all characters based on the one-to-one mapping in the said table.

Syntax

Following is the syntax of Python str.translate() method -

str.translate(table)

Example

Let's look at the following example, where we are going to use the string.punctuation along with the translate() method.

import string
demo = "Welco.me!, T:o T?utorials.,point"
Result = demo.translate(str.maketrans('', '', string.punctuation))
print(Result)

Output

Output of the above program is as follows -

Welcome To Tutorialspoint

Using Python re Module

The python re module supports for the regular expressions, helping in string patterns matching and manipulation. It includes functions like match(), search() to identify, extract or replace patterns in strings.

Example

Consider the following example, where we are going to use the regular expression to remove the punctuations.

import re
text = "Foobar, Baz?"
no_punct = re.sub(r'[^a-zA-Z0-9\s]+', '', text)
print(no_punct)

Output

Output of the above program is as follows -

Foobar Baz

Using Python List Comprehension

The python List comprehension offers a simple way to create a lists using a single line of code. It combines loops and conditional statements making it efficient compared to the traditional methods of list construction.

Syntax

Following is the syntax of python list comprehension -

newlist = [expression for item in iterable if condition == True]

Example

In the following example, we are going to use the list comprehension along with the string.punctuation.

import string
demo = "T.,uto:?rials:point."
result = "".join(char for char in demo if char not in string.punctuation)
print(result)

Output

Following is the output of the above program -

Tutorialspoint

Using Python replace() Method

The Python replace() method replaces all occurrences of one substring in a string with another substring.

Syntax

Following is the syntax of Python replce() method -

str.replace(oldvalue, newvalue, count)

Example

Following is the example, Where we are going to use the replace() method to manually remove the punctuations.

import string
demo = "T.,uto:?rials:point."
result = "".join(char for char in demo if char not in string.punctuation)
print(result)

Output

Following is the output of the above program -

Tutorialspoint
Updated on: 2025-04-16T17:37:48+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements