Python String translate() Method
The translate()
method returns a string where each character is mapped to its corresponding character in the translation table.
The translation table is created by the maketrans() method.
Syntax:
str.translate(table)
Parameters:
table: A translation table containing the mapping between two characters created by the maketrans() method.
Return Value:
Returns a string where each character is mapped to its corresponding character as per the translation table.
Passing Dictionary
The following example demonstrates the translate()
method where maketrans()
has a single parameter.
mystr = 'Tutorials Teacher'
trans_dict = {'T':'1','o':'2','l':'3','h':'4'}
mytable = mystr.maketrans(trans_dict)
mystr_translated = mystr.translate(mytable)
print('Original String:', mystr)
print('Translated String:', mystr_translated)
print('Table:', mytable)
Original String: Tutorials Teacher
Translated String: 1ut2ria3s 1eac4er
Table: {84: '1', 111: '2', 108: '3', 104: '4'}
If there is a single argument, it should be a dictionary. In the above example, the maketrans()
method returns a table that includes Unicode ordinal of the keys of the trans_dict
, which are mapped to their respective values.
The table is then passed to the translate()
method to convert a string by replacing keys with the corresponding values of trans_dict
.
Thus, it converts a string 'Tutorials Teacher'
to '1ut2ria3s 1eac4er'
by replacing 'T', 'o', 'l', 'h' to '1', '2', '3', '4' respectively.
Passing Two Parameters
The following example demonstrates the translate()
method where maketrans()
has two parameters.
mystr = 'Python is a programming language'
str1 = 'Pli'
str2 = 'abc'
mytable = mystr.maketrans(str1, str2)
mystr_translated = mystr.translate(mytable)
print('Original String:', mystr)
print('Translated String:', mystr_translated)
print('Table:', mytable)
Original String: Python is a programming language
Translated String: aython cs a programmcng banguage
Table: {80: 97, 108: 98, 105: 99}
If there are two or more parameters, then all the parameters should be a string. In the above example, the maketrans()
method will return a transition table wherein all the characters of str1
and str2
are displayed in the form of their respective Unicode Ordinals.
Each character in str1
is a replacement to its corresponding index in str2
. The table is then passed to the translate()
method and it will translate the string accordingly.
The following example demonstrates the translate()
method where maketrans()
has all three parameters.
mystr = 'Hello World $%&*'
str1 = 'elod'
str2 = '1234'
str3 = '$%&*'
mytable = mystr.maketrans(str1,str2,str3)
print('Original String:',mystr)
print('Translated String:',mystr.translate(mytable))
Original String: Hello World $%&*
Translated String: H1223 W3r24
If three parameters are passed then all the characters of the third parameter are mapped to none in the transition table returned by the maketrans()
method and all the characters of str1 are replaced by characters of str2.The table is then passed to the translate()
method and it will translate the string accordingly.