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
| Parameter | Description |
|---|---|
table |
A mapping of code points to strings, code points or None (delete), usually from str.maketrans(). |
Return value
A new string.
Examples
leet = str.maketrans({"a": "4", "e": "3", "o": "0"})
print("hello developer".translate(leet))
Output
h3ll0 d3v3l0p3r
import string
no_punct = str.maketrans("", "", string.punctuation)
print("Hello, world! (test)".translate(no_punct))
Output
Hello world test
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.maketrans()· Create a translation table for translate(): map characters to replacements, or to None to delete them.str.replace()· Return a copy with every occurrence of old replaced by new, or only the first count occurrences.- Python Strings · tutorial