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)
Hello World Python is amazingExplanation:
str.maketrans('', '', string.punctuation)
creates a mapping to remove all punctuation.translate()
method applies this mapping efficiently.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)
Text cleaning Regexbased Works fineExplanation:
[^\w\s]
matches any character that is not a word character or whitespace.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)
Hello World Python is amazingExplanation:
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)
Filtering Is it clean now
Explanation:
string.punctuation
.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