Python 3 reference · String Methods

Python String translate() Method

Return a copy where each character is mapped through a translation table, replacing or deleting characters fast.

Python 3 Since 3.0

Syntax

str.translate(table)

Parameters

ParameterDescription
table A mapping of code points to strings, code points or None (delete), usually from str.maketrans().

Return value

A new string.

Examples

Replace charactersTry it yourself
leet = str.maketrans({"a": "4", "e": "3", "o": "0"})
print("hello developer".translate(leet))

Output

h3ll0 d3v3l0p3r
Remove punctuationTry it yourself
import string
no_punct = str.maketrans("", "", string.punctuation)
print("Hello, world! (test)".translate(no_punct))

Output

Hello world test

Version history

VersionChange
Python 3.0Added