Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'abc'
table = str.maketrans({"a": "A", "b": None, "c": "see"})
result = text.translate(table)
print(result)Top-level scope
'abc'Object 2 · dict{97: 'A', 98: None, 99: 'see'}Object 3 · str'Asee'translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.
(no output)
What do you expect this program to print?
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'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'abc'
table = str.maketrans({"a": "A", "b": None, "c": "see"})
result = text.translate(table)
print(result)Top-level scope
'abc'Object 2 · dict{97: 'A', 98: None, 99: 'see'}Object 3 · str'Asee'The example has finished. Compare the final output with the values and references above.
Asee
What do you expect this program to print?
text = 'abc'
table = str.maketrans({"a": "A", "b": None, "c": "see"})
result = text.translate(table)
print(result)translate() MethodThe 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.
string.translate(table)
table – A translation table created using str.maketrans().None).Use the str.maketrans() function to build the table. You can map one character to another, or multiple at once.
table = str.maketrans("abc", "123")
This means: replace 'a' → '1', 'b' → '2', 'c' → '3'
text = "abc cab"
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)
123 312
To remove characters, map them to None.
text = "hello world!"
table = str.maketrans("", "", "aeiou")
result = text.translate(table)
print(result)
hll wrld!
translate() without creating a table using maketrans().str.maketrans("abc", "12") – lengths must match.translate() is highly efficient when cleaning strings, replacing digits, removing punctuation, or transforming inputs based on character rules.
translate() modifies a string using a mapping table.str.maketrans() to create that table.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))
* l*v* Pyth*n!
Connect what you noticed in the example with the idea below.
translate() applies a character mapping. Mapped None values delete characters, and strings can expand one character into several.
translate() only supports one-character replacements.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.