In the previous article, we have discussed Python List index() Method with Examples
translate() Method in Python:
The translate() method returns a string in which certain characters are replaced with the character described in a dictionary or mapping table.
To create a mapping table, use the maketrans() method.
If a character is not found in the dictionary/table, it will not be replaced.
When using a dictionary, you must use ascii codes rather than characters.
Syntax:
string.translate(table)
Parameter
table: This is Required. Either a dictionary or a mapping table describing how to replace.
Return value:
The translate() method returns a string with each character mapped to its corresponding character according to the translation table.
Examples:
Example1:
Input:
Given first String = "pqr" Given second String = "stu" Given third String = "ab" Given string = "pqrs"
Output:
The given string: pqrs The translated string for the given string is: stus
Example2:
Input:
Given first String = "efg" Given second String = "hij" Given third String = "fg" Given string = "efghij"
Output:
The given string: efghij The translated string for the given string is: hhij
String translate() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Give the third string as static input and store it in another variable.
- Give some random string as static input and store it in another variable.
- Print the above-given string.
- Pass the given first, second and third strings as arguments to the maketrans() function for the given string to create a mapping table.
- Store it in another variable.
- Translate the above result for the given string using the translate() function and print it.
- The Exit of the program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "efg" # Give the second string as static input and store it in another variable. gvn_scndstr = "hij" # Give the third string as static input and store it in another variable. gvn_thrdstr = "fg" # Give some random string as static input and store it in another variable. gvn_str = "efghij" # Print the above-given string. print("The given string:", gvn_str) # Pass the given first, second and third strings as arguments to the maketrans() # function for the given string to create a mapping table. # Store it in another variable. translatn = gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr) # Translate the above result for the given string using the translate() # function and print it. print("The translated string for the given string is:", gvn_str.translate(translatn))
Output:
The given string: efghij The translated string for the given string is: hhij
Explanation:
The mapping translation here contains the mapping from e f, and g to h, i, and j respectively.
The removal string third string, on the other hand, resets the mapping to e and f to None.
As a result, when the string is translated with translate(), e and f are removed and g is replaced with idef.
For Further More Explanation on translation Please look at the below article :
Python String maketrans() Method with Examples
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the second string as user input using the input() function and store it in another variable.
- Give the third string as user input using the input() function and store it in another variable.
- Give some random string as user input using the input() function and store it in another variable.
- Print the above-given string.
- Pass the given first, second and third strings as arguments to the maketrans() function for the given string to create a mapping table.
- Store it in another variable.
- Translate the above result for the given string using the translate() function and print it.
- The Exit of the program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some Random String = ") # Give the second string as user input using the input() function and store it in another variable. gvn_scndstr = input("Enter some Random String = ") # Give the third string as user input using the input() function and store it in another variable. gvn_thrdstr = input("Enter some Random String = ") # Give some random string as static input and store it in another variable. gvn_str = input("Enter some Random String = ") # Print the above-given string. print("The given string:", gvn_str) # Pass the given first, second and third strings as arguments to the maketrans() # function for the given string to create a mapping table. # Store it in another variable. translatn = gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr) # Translate the above result for the given string using the translate() # function and print it. print("The translated string for the given string is:", gvn_str.translate(translatn))
Output:
Enter some Random String = pqr Enter some Random String = stu Enter some Random String = ab Enter some Random String = pqrs The given string: pqrs The translated string for the given string is: stus
Translation with translate() with manual translation table(for dictionaries)
Below is the implementation:
gvn_dict = {101: 98, 104: 97, 102: None} gvn_str = "efghij" print("The given string is:", gvn_str) print("The translated string for the given string is:", gvn_str.translate(gvn_dict))
Output:
The given string is: efghij The translated string for the given string is: bgaij
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.