A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-remove-punctuation-from-string/ below:

Python - Remove Punctuation from String

Python - Remove Punctuation from String

Last Updated : 12 Jul, 2025

In this article, we will explore various methods to Remove Punctuations from a string.

Using str.translate() with str.maketrans()

str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation tables.

Python
s = "Hello, World! Python is amazing."

# Create translation table
import string
translator = str.maketrans('', '', string.punctuation)

# Remove punctuation
clean_text = s.translate(translator)
print(clean_text)

Output
Hello World Python is amazing
Explanation:

Let's explore some more ways and see how we can remove punctuation from string.

Using Regular Expressions (re.sub)

This method is slightly less efficient but very flexible as it Works for complex patterns, not just punctuation.

Python
s = "Text cleaning? Regex-based! Works fine."

# Removing punctuation
import re
clean = re.sub(r'[^\w\s]', '', s)
print(clean)

Output
Text cleaning Regexbased Works fine
Explanation: Using List Comprehension

A concise way to remove punctuation is by iterating through the string and including only alphanumeric characters and spaces.

Python
s = "Hello, World! Python is amazing."
import string
# Filter out punctuation
clean = ''.join([char for char in s if char not in string.punctuation])
print(clean)

Output
Hello World Python is amazing
Explanation: Using filter()

filter() function can also be used for removing punctuation, providing a clean and functional approach.

Python
s = "Filtering... Is it clean now?"

# Removing punctuation
import string
clean = ''.join(filter(lambda x: x not in string.punctuation, s))
print(clean)

Output
Filtering Is it clean now

Explanation:


Python program to Remove Punctuation from String


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4