str.maketrans() Translation table
Last updated: 2026-09-22
str.maketrans() Translation table
str.maketrans(x[, y[, z]]) returns a translation table for str.translate().
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.maketrans(x[, y[, z]])
⚙️ Parameters
| x Required | As a single arg, a char-to-char dict; with y/z, the source string to map. |
|---|---|
| y Optional | A string of equal length to x, mapping chars one-to-one.(Default:None) |
| z Optional | A string of chars to delete from the result.(Default:None) |
Returns:dict. A translation table usable by str.translate().
Mutates the original:No (returns a new object)
💥 Raises
TypeError— Raised when x is the only argument and is not a dict.ValueError— Raised when x and y have different lengths.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
h2ll4 w4rld
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat are the three calling forms?
Astr.maketrans(x): x is a character-to-character mapping dict; str.maketrans(x, y): x and y are equal-length strings mapped character by character; str.maketrans(x, y, z): z is the set of characters to delete.
QWhat happens when x and y have different lengths?
AA ValueError is raised; x and y must be the same length.
QHow is the generated translation table used?
APass it to str.translate(): tbl = str.maketrans('aeiou', '12345'), then 'hello'.translate(tbl).