Translation Methods



maketrans()

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.

Syntax

 var.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.

Return Value

This method returns a translate table to be used translate() function.

Example

 var = '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.

Syntax

 var.translate(table) 

Parameters

  • table − A translation table created by the maketrans() method.

Return Value

This method returns a translated copy of the string.

Example

 var = '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
Advertisements