Remember
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.
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("abc", "123")
result = text.translate(table)
print(result)Top-level scope
'abc'Object 2 · dict{97: 49, 98: 50, 99: 51}Object 3 · str'123'str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.
(no output)
What do you expect this program to print?
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'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("abc", "123")
result = text.translate(table)
print(result)Top-level scope
'abc'Object 2 · dict{97: 49, 98: 50, 99: 51}Object 3 · str'123'The example has finished. Compare the final output with the values and references above.
123
What do you expect this program to print?
text = 'abc'
table = str.maketrans("abc", "123")
result = text.translate(table)
print(result)maketrans() MethodThe 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.
str.maketrans(x, y, z)
x: A dictionary or a string containing characters to be replacedy: A string with characters to replace x (must be the same length)z: (Optional) A string of characters to be removedstr.translate()table = str.maketrans("aeiou", "12345")
text = "hello world"
translated = text.translate(table)
print(translated)
h2ll4 w4rld
table = str.maketrans("", "", "aeiou")
text = "hello world"
translated = text.translate(table)
print(translated)
hll wrld
table = str.maketrans({97: 49, 98: 50}) # a → 1, b → 2
text = "abc"
print(text.translate(table))
12c
translate() for powerful string manipulationx and y are strings, they must be of equal length.translate() on the actual string—not on the translation table.In coding rounds, maketrans() + translate() is often used for character substitution problems like encoding, sanitizing input, or removing unwanted characters.
maketrans() creates a translation tabletranslate() to modify stringsWrite 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))
Connect what you noticed in the example with the idea below.
str.maketrans() builds a translation mapping. translate() uses it to replace or remove characters.
maketrans() changes the string immediately.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.