The maketrans() method returns a mapping table. It maps each character from astr to a character at same index in bstr. The mapping table returned by this method may be used by translate() method to replace the characters.
Syntaxvar.maketrans(astr, bstr, cstr)Parameters
astr − This can be either dictionary or string. If only one parameter is supplied, this parameter must be a dictionary. If two or more parameters are given, this parameter has to be a string specifying the characters to be replaced.
bstr − This is the string having corresponding mapping character.
cstr − Optional. The string parameter specifies the characters to remove in the string.
This method returns a translate table to be used translate() function.
Examplevar = 'Explicit is better than implicit.' table = var.maketrans({'i':'I'}) print ("original string:", var) print ("translation table:", table) var1 = var.translate(table) print ("Translated string", var1) var = "Explicit is better than implicit." table = var.maketrans("than", "then") print ("original string:", var) print ("translation table:", table) var2 = var.translate(table) print ("Translated string", var2) var = 'Explicit is better than implicit.' table = var.maketrans("is","as", "s") print ("original string:", var) print ("translation table:", table) var3=var.translate(table) print ("Translated string", var3)
When you run this program, it will produce the following output −
original string: Explicit is better than implicit. translation table: {105: 'I'} Translated string ExplIcIt Is better than ImplIcIt. original string: Explicit is better than implicit. translation table: {116: 116, 104: 104, 97: 101, 110: 110} Translated string Explicit is better then implicit. original string: Explicit is better than implicit. translation table: {105: 97, 115: None} Translated string Explacat a better than amplacat.translate()
The translate() method returns a string where each character is replaced by its corresponding character in the translation table created by the maketrans() method.
Syntaxvar.translate(table)Parameters
table − A translation table created by the maketrans() method.
This method returns a translated copy of the string.
Examplevar = 'Explicit is better than implicit.' table = var.maketrans({'i':'I'}) print ("original string:", var) print ("translation table:", table) var1 = var.translate(table) print ("Translated string", var1) var = "Explicit is better than implicit." table = var.maketrans("than", "then") print ("original string:", var) print ("translation table:", table) var2 = var.translate(table) print ("Translated string", var2) var = 'Explicit is better than implicit.' table = var.maketrans("is","as", "s") print ("original string:", var) print ("translation table:", table) var3=var.translate(table) print ("Translated string", var3)
When you run this program, it will produce the following output −
original string: Explicit is better than implicit. translation table: {105: 'I'} Translated string ExplIcIt Is better than ImplIcIt. original string: Explicit is better than implicit. translation table: {116: 116, 104: 104, 97: 101, 110: 110} Translated string Explicit is better then implicit. original string: Explicit is better than implicit. translation table: {105: 97, 115: None} Translated string Explacat a better than amplacat.
python_string_methods.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4