Python maketrans() method
Description
The Python maketrans() method creates a translation table for character mapping. In its simplest form, which takes two arguments, the first is a string representing the characters to convert, and the second is also a string representing the conversion targets.
**Note:** The two strings must be of the same length, forming a one-to-one correspondence.
Syntax
Syntax of the maketrans() method:
str.maketrans(intab, outtab)
Parameters
- intab -- a string consisting of the characters to be replaced.
- outtab -- a string of the corresponding mapping characters.
Return Value
Returns the new string generated after the translation.
Example
The following example demonstrates using the maketrans() method to convert all vowels into specified digits:
Example
#!/usr/bin/python
-- coding: UTF-8 --
from
string
import
maketrans
The maketrans function must be called.
intab
=
"aeiou"
outtab
=
"12345"
trantab
=
maketrans
(
intab
,
outtab
)
str
=
"this is string example....wow!!!"
;
str
.
translate
(
trantab
)
;
The output of the above example is:
th3s 3s str3ng 2x1mpl2....w4w!!!
Python Strings