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

ParameterDescription
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

Replace characters one-for-oneTry it yourself
table = str.maketrans("abc", "xyz")
print("aabbcc".translate(table))

Output

xxyyzz
Delete charactersTry it yourself
no_vowels = str.maketrans("", "", "aeiou")
print("programming".translate(no_vowels))

Output

prgrmmng

Version history

VersionChange
Python 3.0Added