Python translate method
Python translate() method :
In this tutorial, we will learn how to replace multiple characters of a string with a different set of string. This is also known as the translation of a string. Python provides one inbuilt method to do the translation. I will show you one example below to implement this operation in python.
translate() method, as its name suggests, used to translate a string. Or, this is used to replace a set of characters with a different set of characters in a string. Both of the character set is mapped using a table. It takes one parameter, i.e. the mapping table and modifies the string as mentioned in that table. This function is included in the string class and we can call it directly on any string.
For creating the mapping table, one function maketrans() is used. The syntax of this function is as below :
It will map each character of the firstString to secondString. i.e. if the firstString is ‘abcde’ and the secondString is ‘12345’, ‘a’ will be mapped with ‘1’, ‘b’ will be mapped with ‘2’ etc. in the mapping table. Note that both strings should have equal length. The maketrans() method returns one string containing this mapping info. We will use this mapping info in the ‘translate()’ method.
Now, let’s come back to the translate() method. This method takes one parameter :
A string that is returned by the maketrans method or the mapping info. So, for translating a string, we need to execute the following two steps :
1. Create one mapping info table using the ‘maketrans()’ method
2. Find out the translation of the string using the ‘translate()’ method
Let me show you with examples to make it more clear (The source code for the below examples are available here) :
Example to use translate() :
In this example, we will simply replace a few characters of a string with different characters :
Here, ‘given_str’ is the original string. Our goal is to replace ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’ in this string with ‘1’, ‘2’, ‘3’, ‘4’, and ‘5’ respectively.
Our first step is to create the mapping info table using maketrans method. maketrans() method takes two string parameters. In our example,’abcde’ is the first parameter and ‘12345’ is the second parameter. All characters in the first parameter will be replaced with the same indexed characters of the second string parameter. The ‘maketrans’ method creates the mapping info table and this info is stored in the ‘table’ variable in the above example.
Our second step is to build the final translated string using the mapping info generated by the maketrans() method. translate() method is used for this and we are passing the ‘table’ as the parameter in the example above. As you can see that characters a,b,c,d,e are replaced with 1,2,3,4,5 respectively as we have used them in maketrans() method.
As you can see that characters a,b,c,d,e are replaced with 1,2,3,4,5 respectively in the final string.
Note that both string variables of the ‘maketrans()’ method must be the same. If you try to pass two strings with different length, it will throw one exception like below :
This tutorial has explained to you how to use python translate and maketrans methods. Always pass two strings with equal lengths to avoid the exception. Note that this tutorial is for python 3. On Python 2, translate behaves a little bit differently.
You might also like :