How to Remove Punctuation from a String in Python
What you’ll build or solve
You’ll remove punctuation from a Python string so the text is easier to search, compare, or split into words.
When this approach works best
Removing punctuation works best when you want to:
Learn Python on Mimo
- Clean text before splitting it into words, like preparing search keywords.
- Normalize user input, like comparing titles that may include commas, quotes, or extra symbols.
- Preprocess text for simple analysis, like counting words or building a frequency list.
This is a bad idea when punctuation carries meaning, like email addresses, decimals, code snippets, or identifiers such as snake_case. In those cases, remove only specific characters.
Prerequisites
- Python 3 installed
- You know what a string variable is
Step-by-step instructions
1) Remove standard punctuation with translate()
For most cases, remove punctuation by mapping punctuation characters to None.
Python
import string
text = "Hi, Jovana! Ready for Python 3.12?"
clean = text.translate(str.maketrans("", "", string.punctuation))
print(clean)
What to look for: string.punctuation covers ASCII punctuation. If your text includes Unicode punctuation like smart quotes, remove those separately.
2) Keep certain punctuation with a filter
If you want to keep some punctuation, filter character by character so your rule stays clear.
Python
import string
text = "rock'n'roll, 80s-style!"
keep = {"'", "-"} # keep apostrophes and hyphens
drop = set(string.punctuation) - keep
clean = "".join(ch for ch in text if ch not in drop)
print(clean)
This approach works well for IDs, names, and product codes where a couple of punctuation marks still matter.
Examples you can copy
Example 1: Clean a sentence for keyword matching
Python
import string
query = "Best pizza, Boston!!!"
clean = query.lower().translate(str.maketrans("", "", string.punctuation))
print(clean)
Example 2: Keep hyphens in product codes
Python
import string
code = "BK-123/45"
keep = {"-"}
drop = set(string.punctuation) - keep
clean = "".join(ch for ch in code if ch not in drop)
print(clean) # BK-12345
Example 3: Keep apostrophes in names
Python
import string
name = "O'Connor, Sean."
keep = {"'"}
drop = set(string.punctuation) - keep
clean = "".join(ch for ch in name if ch not in drop)
print(clean) # O'Connor Sean
Example 4: Remove “smart quotes” and dashes too
Python
import string
text = "“Don’t” use — random punctuation… please."
extra = "“”’…—" # add any characters you want to remove
all_punct = string.punctuation + extra
clean = text.translate(str.maketrans("", "", all_punct))
print(clean)
Example 5: Alternative approach using regex for complex Unicode
Python
import re
text = "Hi… this — has weird punctuation “too”."
clean = re.sub(r"[^\w\s]", "", text)
print(clean)
What to look for: \w includes letters, digits, and underscores. If you want to remove underscores too, strip them after:
Python
clean = clean.replace("_", "")
print(clean)
Common mistakes and how to fix them
Mistake 1: Removing punctuation that you actually need
What you might do
Python
import string
email = "ana.maria@example.com"
clean = email.translate(str.maketrans("", "", string.punctuation))
print(clean)
Why it breaks
You removed @ and ., so the result no longer works as an email address.
Corrected approach
Skip punctuation removal for fields like emails, or remove only the characters you want to drop.
Python
text = "Contact: ana.maria@example.com!"
clean = text.replace("!", "")
print(clean)
Mistake 2: Expecting string.punctuation to remove all punctuation
What you might do
Python
import string
text = "Hi… “quoted” text."
clean = text.translate(str.maketrans("", "", string.punctuation))
print(clean)
Why it breaks
ASCII punctuation does not include many Unicode characters like … or “.
Corrected approach
Add the extra characters you see in your data.
Python
import string
text = "Hi… “quoted” text."
extra = "…“”"
clean = text.translate(str.maketrans("", "", string.punctuation + extra))
print(clean)
Troubleshooting
If punctuation stays in the output, do this:
Print repr(text) to see the exact characters. Add them to your removal list.
Python
text = "Hi…"
print(repr(text))
If you removed hyphens or apostrophes you wanted to keep, do this:
Use the filter approach with a keep set.
If your cleaned text has double spaces, do this:
Collapse whitespace after cleaning:
Python
import re
clean = re.sub(r"\s+", " ", clean).strip()
print(clean)
Quick recap
- Use
translate()withstring.punctuationfor quick ASCII punctuation removal. - Use a filter when you want to keep punctuation like
'. - Add Unicode punctuation characters to your removal list when needed.
- Use regex as an alternative when your data has many non-ASCII symbols.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot