Python translate() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

translate() goes through a string one character at a time and uses a translation table to decide what to do with each one: replace it, delete it, or leave it alone. Characters that are not in the table are copied unchanged.

The table is almost always built with maketrans(). It is the best tool when you want to change many single characters in one pass, for example to remove all punctuation. The method returns a new string.

text.translate() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass a translation table. It is required.

python
result = text.translate(table)
CallWhat it does
"hello".translate(str.maketrans("el", "ip"))Returns 'hippo'. Each e becomes i, and each l becomes p.
"a-b".translate({ord("-"): None})Returns 'ab'. A value of None deletes the character.
"a b".translate({ord(" "): "__"})Returns 'a__b'. A value can be a string of any length.

To replace one fixed piece of text use replace(). To build a table, use maketrans(). To remove only the ends of a string, use strip().

Library declarations
python
str.translate(table) -> str
# returns: a new string with the table applied
# table: a dict or any object that supports lookup by code number

Parameters

ParameterDescription
tableRequired. A dictionary, usually made by str.maketrans(), whose keys are Unicode code numbers. Its values are replacement strings, code numbers, or None to delete.

Return Value

Return ValueDescription
strA new string with the table applied. Characters that are not in the table are kept. The original string is not changed.

Python Examples

Example 1 — Replace Vowels With Numbers

Build a table with maketrans() and use it on two words.

python
table = str.maketrans("aeiou", "12345")

print("education".translate(table))
print("Python".translate(table))
print("education")

How It Works

  1. Build the table. a becomes 1, e becomes 2, i becomes 3, o becomes 4, and u becomes 5.
  2. Apply it. Every vowel in education is replaced, and the consonants are copied unchanged.
  3. Other words work too. In Python, only the o changes, and the capital P is not in the table.
  4. The original stays. The last line still shows education.

Example 2 — Strip All Punctuation

Use the string module to delete every punctuation mark, then change the case.

python
import string

text = "Hello, World! (2024)"
table = str.maketrans("", "", string.punctuation)

print(text.translate(table))
print(text.translate(table).lower())

How It Works

  1. A ready-made set. string.punctuation holds all the common punctuation characters.
  2. Delete them. Passing them as the third argument of maketrans() maps each one to None.
  3. One pass over the text. The comma, the exclamation mark, and the brackets disappear, while the spaces and digits stay.
  4. Chain another method. The result is a string, so lower() can be called on it.

Example 3 — Shift Letters by Three

Build a table that shifts every letter three places, and use a second table to undo it.

python
import string

letters = string.ascii_lowercase
shifted = letters[3:] + letters[:3]
table = str.maketrans(letters, shifted)

secret = "hello world".translate(table)
print(secret)

undo = str.maketrans(shifted, letters)
print(secret.translate(undo))

How It Works

  1. Shift the alphabet. letters[3:] + letters[:3] moves the first three letters to the end, so a maps to d.
  2. Encode. translate() replaces every letter, and the space is not in the table, so it stays.
  3. Build the reverse table. Swapping the two strings in maketrans() gives a table that undoes the shift.
  4. Decode. Applying the reverse table brings back hello world.

Frequently Asked Questions

It returns a copy of the string in which each character is replaced, or deleted, according to a translation table.
Use str.maketrans(). For example, str.maketrans("ab", "xy") maps a to x and b to y.
Make a table with str.maketrans("", "", string.punctuation) and pass it to translate(). Import the string module first.
replace() swaps one piece of text for another. translate() changes many single characters in one pass, using a table.
The keys must be Unicode code numbers, such as ord("a"), and not characters. Build the table with str.maketrans() to avoid this.

Next: upper()

Return a copy of the string with every letter in upper case.

upper() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful