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 :
- Create one mapping info table using the ‘maketrans()’ method
- 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 also available here) :
In this example, we will simply replace a few characters of a string with different characters :
given_str = "The quick brown fox jumps over the lazy dog" table = str.maketrans("abcde","12345") print("Given string : ",given_str) print("String after replacing the characters : ",given_str.translate(table))
Here, ‘givenstr’ 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.