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:

  • 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.

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.

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

import string

query = "Best pizza, Boston!!!"
clean = query.lower().translate(str.maketrans("", "", string.punctuation))
print(clean)

Example 2: Keep hyphens in product codes

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

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

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

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:

clean = clean.replace("_", "")
print(clean)

Common mistakes and how to fix them

Mistake 1: Removing punctuation that you actually need

What you might do

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.

text = "Contact: ana.maria@example.com!"
clean = text.replace("!", "")
print(clean)

Mistake 2: Expecting string.punctuation to remove all punctuation

What you might do

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.

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.

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:

import re

clean = re.sub(r"\s+", " ", clean).strip()
print(clean)

Quick recap

  • Use translate() with string.punctuation for 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.