Remove Punctuation in Python With translate

Learn how to remove punctuation from text in Python using str.translate and str.maketrans, with practical examples for Python 3 strings.

Before you start

This method runs locally in a Python 3 environment. You need a string containing punctuation and a Python interpreter that supports str.maketrans. No third-party packages or online services are required.

The approach uses two built-in string features. str.maketrans builds a translation table that maps selected characters to None. str.translate then applies that table to the text and removes every mapped character. This keeps the operation fast and avoids looping through the string character by character.

Step-by-step instructions

Open your Python 3 file or interactive session and prepare the input text. In this example, the text includes commas, periods, and exclamation marks:

text = "Hello, this is an example!"

Import the string module so you can use its punctuation constant. This constant contains common ASCII punctuation characters:

import string

Create a translation table with str.maketrans. The first two arguments map one character to another, and the third argument contains characters that should be removed. Pass the punctuation set as the third argument and use empty strings for the first two:

translator = str.maketrans("", "", string.punctuation)

Apply the table to the text with str.translate. The result is a new string with the punctuation characters removed:

clean_text = text.translate(translator)

If you need to remove only a specific set of punctuation, replace string.punctuation with a custom string:

translator = str.maketrans("", "", ",.!?")

This is useful when you want to keep characters such as hyphens or apostrophes that appear inside words.

Check the output

Print the result and compare it with the original string:

print(clean_text)

For the example above, the output is "Hello this is an example". Spaces remain unchanged because whitespace is not part of string.punctuation.

A limitation to be aware of is that string.punctuation covers ASCII punctuation. It does not include every Unicode punctuation mark, so characters such as an em dash may remain. If your text contains curly quotes or other Unicode symbols, add them to the third argument explicitly or use a separate cleaning step.

Common problems and next steps

A frequent issue is using maketrans as a standalone function instead of str.maketrans. In Python 3, call str.maketrans with three arguments for character removal. A two-argument call creates a mapping table for replacement, not deletion, so punctuation would remain unchanged.

Another common mistake is passing a set or list of punctuation to maketrans. Convert it to a string first. For example, use "".join(punctuation_set) if you are working with a set.

If punctuation still appears in the output, confirm that the characters are present in the third argument and that the text is a str object rather than bytes. For bytes input, use a bytes translation table with bytes.translate instead.

When the cleaned output must feed into search, transcription, or filename handling, consider a small function that applies the same translation table. This keeps punctuation removal consistent across multiple strings without repeating the table setup.