Python Reference Library →

Python String maketrans() Method – Create Translation Tables

Let’s look at string maketrans() method – create translation tables

  1. Notice

    str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'abc'
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)
Live state

Top-level scope

text→Object 1
table→Object 2
result→Object 3
Object 1 · str'abc'Object 2 · dict{97: 49, 98: 50, 99: 51}Object 3 · str'123'
This step

str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

Picture string maketrans() method – create translation tables

str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

Image brief

Alt text
A diagram for Python String maketrans() Method – Create Translation Tables. Instructions: text = 'abc' table = str.maketrans("abc", "123") result = text.translate(table) print(result). Displayed result: 123. The final example bindings are text --> object 1 (str); table --> object 2 (dict); result --> object 3 (str). object 1: 'abc'; object 2: {97: 49, 98: 50, 99: 51}; object 3: '123'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String maketrans() Method – Create Translation Tables". Teaching focus: str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'abc' table = str.maketrans("abc", "123") result = text.translate(table) print(result) | v DISPLAYED RESULT 123 FINAL BINDINGS text --> object 1 (str) table --> object 2 (dict) result --> object 3 (str) object 1: 'abc' object 2: {97: 49, 98: 50, 99: 51} object 3: '123' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String maketrans() Method – Create Translation Tables. Instructions: text = 'abc' table = str.maketrans("abc", "123") result = text.translate(table) print(result). Displayed result: 123. The final example bindings are text --> object 1 (str); table --> object 2 (dict); result --> object 3 (str). object 1: 'abc'; object 2: {97: 49, 98: 50, 99: 51}; object 3: '123'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'abc'
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)
Live state

Top-level scope

text→Object 1
table→Object 2
result→Object 3
Object 1 · str'abc'Object 2 · dict{97: 49, 98: 50, 99: 51}Object 3 · str'123'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
123

What do you expect this program to print?

What does this example display?

text = 'abc'
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

123

Follow the printed values in order. The output is: 123

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

More examples and reference

Python String maketrans() Method

The maketrans() method is a built-in function used to create a translation table. This table can then be used with the translate() method to replace specified characters in a string with new characters.

Syntax

str.maketrans(x, y, z)

Parameters:

  • x: A dictionary or a string containing characters to be replaced
  • y: A string with characters to replace x (must be the same length)
  • z: (Optional) A string of characters to be removed

Returns:

  • A translation table that can be used with str.translate()

Example 1: Replace Characters Using maketrans()

table = str.maketrans("aeiou", "12345")
text = "hello world"
translated = text.translate(table)
print(translated)
h2ll4 w4rld

Example 2: Remove Characters Using maketrans()

table = str.maketrans("", "", "aeiou")
text = "hello world"
translated = text.translate(table)
print(translated)
hll wrld

Example 3: Use Dictionary in maketrans()

table = str.maketrans({97: 49, 98: 50})  # a → 1, b → 2
text = "abc"
print(text.translate(table))
12c

Use Case: Why Use maketrans()?

  • Efficient way to substitute multiple characters at once
  • Used in text preprocessing, encryption, and formatting
  • Combines cleanly with translate() for powerful string manipulation

Common Mistakes

  • Mismatch Length: If x and y are strings, they must be of equal length.
  • Wrong Type: Don't forget to call translate() on the actual string—not on the translation table.

Interview Tip

In coding rounds, maketrans() + translate() is often used for character substitution problems like encoding, sanitizing input, or removing unwanted characters.

Summary

  • maketrans() creates a translation table
  • Used with translate() to modify strings
  • Supports string mapping and character removal

Practice Problem

Write a program that replaces all vowels in user input with * using maketrans().

text = input("Enter some text: ")
table = str.maketrans("aeiouAEIOU", "**********")
print(text.translate(table))

What to remember

Connect what you noticed in the example with the idea below.

Remember

str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.

Watch out for this idea

maketrans() changes the string immediately.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →