Python Reference Library →

Python String translate() Method – Replace Characters Using Mapping

Let’s look at string translate() method – replace characters using mapping

  1. Notice

    translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

  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({"a": "A", "b": None, "c": "see"})
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: 'A', 98: None, 99: 'see'}Object 3 · str'Asee'
This step

translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

Picture string translate() method – replace characters using mapping

translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

Image brief

Alt text
A diagram for Python String translate() Method – Replace Characters Using Mapping. Instructions: text = 'abc' table = str.maketrans({"a": "A", "b": None, "c": "see"}) result = text.translate(table) print(result). Displayed result: Asee. The final example bindings are text --> object 1 (str); table --> object 2 (dict); result --> object 3 (str). object 1: 'abc'; object 2: {97: 'A', 98: None, 99: 'see'}; object 3: 'Asee'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String translate() Method – Replace Characters Using Mapping". Teaching focus: translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'abc' table = str.maketrans({"a": "A", "b": None, "c": "see"}) result = text.translate(table) print(result) | v DISPLAYED RESULT Asee FINAL BINDINGS text --> object 1 (str) table --> object 2 (dict) result --> object 3 (str) object 1: 'abc' object 2: {97: 'A', 98: None, 99: 'see'} object 3: 'Asee' 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 translate() Method – Replace Characters Using Mapping. Instructions: text = 'abc' table = str.maketrans({"a": "A", "b": None, "c": "see"}) result = text.translate(table) print(result). Displayed result: Asee. The final example bindings are text --> object 1 (str); table --> object 2 (dict); result --> object 3 (str). object 1: 'abc'; object 2: {97: 'A', 98: None, 99: 'see'}; object 3: 'Asee'

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({"a": "A", "b": None, "c": "see"})
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: 'A', 98: None, 99: 'see'}Object 3 · str'Asee'
This step

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

Terminal
Output
Asee

What do you expect this program to print?

What does this example display?

text = 'abc'
table = str.maketrans({"a": "A", "b": None, "c": "see"})
result = text.translate(table)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

Asee

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

Which explanation matches what you learned?

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

Answer and explanation

translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

More examples and reference

Python String translate() Method

The translate() method returns a new string in which characters have been replaced using a translation table. This is a powerful method when you want to replace or remove multiple characters in a single operation.

Syntax

string.translate(table)

Parameters:

  • table – A translation table created using str.maketrans().

Returns:

  • A new string with specified characters replaced (or removed if mapped to None).

How to Create a Translation Table

Use the str.maketrans() function to build the table. You can map one character to another, or multiple at once.

Example:

table = str.maketrans("abc", "123")

This means: replace 'a' → '1', 'b' → '2', 'c' → '3'

Example: Basic Character Replacement

text = "abc cab"
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)
123 312

Example: Remove Characters

To remove characters, map them to None.

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

Use Case: Replacing or Deleting Multiple Characters

  • Efficient for replacing multiple characters in one go.
  • Great for creating filters, obfuscation, or cleaning up text data.

Common Mistakes

  • Using translate() without creating a table using maketrans().
  • Mismatch in string lengths when using str.maketrans("abc", "12") – lengths must match.

Interview Tip

translate() is highly efficient when cleaning strings, replacing digits, removing punctuation, or transforming inputs based on character rules.

Summary

  • translate() modifies a string using a mapping table.
  • Use str.maketrans() to create that table.
  • Supports both character replacement and deletion.

Practice Problem

Write a program that replaces all vowels in a string with '*' using translate().

text = "I love Python!"
vowels = "aeiouAEIOU"
replace_with = "*" * len(vowels)
table = str.maketrans(vowels, replace_with)
print(text.translate(table))

Expected Output:

* l*v* Pyth*n!

What to remember

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

Remember

translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.

Watch out for this idea

translate() only supports one-character replacements.

This is the misconception to avoid.

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

    Reference library →