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 CityI have executed the above example code and added the screenshot below.

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 CAI have executed the above example code and added the screenshot below.

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 weekI have executed the above example code and added the screenshot below.

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 599Here, 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:
- How to Reverse a String in Python?
- Find the First Number in a String in Python
- How to Compare Strings in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.