The string translate[] method returns a string where each character is mapped to its corresponding character in the translation table.
translate[]
method takes the translation table to replace/translate characters in the given string as per the mapping table.
The translation table is created by the static method maketrans[].
The syntax of the translate[]
method is:
string.translate[table]
String translate[] Parameters
translate[]
method takes a single parameter:
- table - a translation table containing the mapping between two characters; usually created by
maketrans[]
Return value from String translate[]
translate[]
method returns a string where each character
is mapped to its corresponding character as per the translation table.
Example 1: Translation/Mapping using a translation table with translate[]
# first string
firstString = "abc"
secondString = "ghi"
thirdString = "ab"
string = "abcdef"
print["Original string:", string]
translation = string.maketrans[firstString, secondString, thirdString]
# translate string
print["Translated string:", string.translate[translation]]
Output
Original string: abcdef Translated string: idef
Here, the translation mapping translation contains the mapping from a, b and c
to g, h and i
respectively.
But, the removal string thirdString resets the mapping to a
and b
to None
.
So, when the string is translated using translate[]
, a
and
b
are removed, and c
is replaced i
outputting idef
.
Note: If you cannot understand what's going inside maketrans[], please refer to String maketrans[].
Example 2: Translation/Mapping with translate[] with manual translation table
# translation table - a dictionary
translation = {97: None, 98: None, 99: 105}
string = "abcdef"
print["Original string:", string]
# translate string
print["Translated string:", string.translate[translation]]
Output
Original string: abcdef Translated string: idef
Here, we don't create a translation table from maketrans[]
but, we manually
create the mapping dictionary translation.
This translation is then used to translate string
to get the same output as the previous example.
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.
Want to check how much you know Python?