Python maketrans() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

maketrans() prepares a translation table: a dictionary that says what to do with each character. You build it once, and then give it to translate(), which applies it to a string.

It has three call forms: two strings of equal length (each character of the first becomes the matching character of the second), the same with a third string of characters to delete, or a single dictionary. The table uses Unicode code numbers as keys, which is why a printed table looks like {97: 120} and not like {"a": "x"}.

str.maketrans() is a static method of the built-in str type: call it on the type itself, as in str.maketrans(...), not on a string. No import is needed.

Syntax

Choose one of the three forms. Always call it on str.

python
table = str.maketrans(x)
table = str.maketrans(x, y)
table = str.maketrans(x, y, z)
CallWhat it does
str.maketrans("ab", "xy")Returns {97: 120, 98: 121}. The code numbers of a and b map to the code numbers of x and y.
str.maketrans("a", "b", "c")Returns {97: 98, 99: None}. The third string adds a deletion: c maps to None.
str.maketrans("a", "xy")Raises ValueError. With two strings, they must have the same length.

The table is only useful together with translate(). For one replacement of fixed text, replace() is simpler.

Library declarations
python
str.maketrans(x[, y[, z]]) -> dict
# returns: a dict that maps Unicode code numbers to replacements
# raises: ValueError if x and y differ in length

Parameters

ParameterDescription
xRequired. With one argument, a dictionary: its keys are single characters or code numbers, and its values are strings, code numbers, or None. With two or three arguments, a string of characters to replace.
yOptional. A string of the same length as x. The character at each position replaces the character at the same position in x.
zOptional. A string of characters to delete. Each one is mapped to None.

Return Value

Return ValueDescription
dictA translation table whose keys are Unicode code numbers (integers), and whose values are replacement strings, code numbers, or None.

Python Examples

Example 1 — Build and Use a Table

Make a table from two strings, look inside it, and apply it with translate().

python
table = str.maketrans("abc", "xyz")

print(table)
print(table[ord("a")])
print("aabbcc".translate(table))

How It Works

  1. A dictionary of numbers. The keys 97, 98, and 99 are the code numbers of a, b, and c.
  2. Look up one letter. ord("a") gives 97, and the table maps it to 120, the code number of x.
  3. Apply the table. translate() replaces every a with x, every b with y, and every c with z.

Example 2 — Use the Third Argument

Delete characters by passing them as the third string.

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

print(table[ord("a")])
print("education".translate(table))

mixed = str.maketrans("-", " ", "!?")
print("go-go!".translate(mixed))

How It Works

  1. Empty first strings. Nothing is replaced, and all five vowels from the third string are marked for deletion.
  2. None means delete. The table maps the code of a to None.
  3. Vowels disappear. education becomes dctn.
  4. Replace and delete together. The hyphen becomes a space, and the exclamation mark is deleted.

Example 3 — Replace Characters With Words

Use the dictionary form to replace single characters by longer text.

python
table = str.maketrans({"&": "and", "<": "(", ">": ")"})

print(table)
print("a<b & c>d".translate(table))

How It Works

  1. Keys become numbers. The characters &, <, and > are stored by their code numbers, 38, 60, and 62.
  2. Values can be longer. The dictionary form lets one character become several, such as & becoming and.
  3. Apply the table. translate() changes all three kinds of characters in one pass.

Frequently Asked Questions

It builds a translation table, a dictionary that maps characters to replacements or to None. You pass the table to the translate() method.
translate() looks characters up by their Unicode code number, so maketrans() converts each character to its code number, as ord() does.
Pass the characters to delete as the third argument, for example str.maketrans("", "", "aeiou").
With two strings they must have the same length. With a dictionary, string keys must be a single character long.
Yes. You call it on the str type, as in str.maketrans(...), and it does not need a string to work on.

Next: partition()

Split a string at the first separator into a tuple of three parts: before, separator, after.

partition() 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.

16 people found this page helpful