Python 3 reference · String Methods
Python String maketrans() Method
Create a translation table for translate(): map characters to replacements, or to None to delete them.
Python 3 Since 3.0
Syntax
str.maketrans(x)
str.maketrans(x, y)
str.maketrans(x, y, z)
Parameters
| Parameter | Description |
|---|---|
x |
A dict of characters to replacements, or a string of characters to replace. |
y optional |
A string of replacement characters, the same length as x. |
z optional |
A string of characters to delete. |
Return value
A dict usable with str.translate().
Examples
table = str.maketrans("abc", "xyz")
print("aabbcc".translate(table))
Output
xxyyzz
no_vowels = str.maketrans("", "", "aeiou")
print("programming".translate(no_vowels))
Output
prgrmmng
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.translate()· Return a copy where each character is mapped through a translation table, replacing or deleting characters fast.str.replace()· Return a copy with every occurrence of old replaced by new, or only the first count occurrences.- Python Strings · tutorial