Python String translate() method returns a modified string replacing the characters described in a dictionary, or in a hash table.
Syntax: string.translate(hash map)
Parameters
- string- Original StringĀ
- hash map- mapping between two characters in the original string.
Python String translate() method Examples
Example 1:
Python3
# hash map trans_dic = { 101 : None , 102 : None , 103 : 105 } original_str = "neveropen" print ( "Original string:" , original_str) # Altered string print ( "Modified string:" , original_str.translate(trans_dic)) |
Output:
Original string:neveropen Modified string: iksoriks
Explanation:
Here āeā and āfā are mapped with None and similarly āgā is mapped with āiā modifying the string to āiksoriksā.
Example 2:
Python3
s = "neveropen" trans = s.maketrans( "G" , "g" ) print (s.translate(trans)) |
Output:
neveropen
Explanation:
Here āGā is mapped with āgā with maketrans() method.
Example 3:
Python3
# strings str1 = "gfg" str2 = "abc" str3 = "gf" original_string = "neveropen" print ( "Initial string:" , original_string) translation = original_string.maketrans(str1, str2, str3) # Altered String print ( "Modified string:" , original_string.translate(translation)) |
Output:
Initial string:neveropen Modified string: eeksoreeks
Explanation:
Here g, f and g are mapped with a, b, and c by maketrans() method and similarly g, f are mapped to None modifying the string to āeeksoreeksā.
Note:Ā
- If the specified character is not in the hash table, the character will not be replaced.
- If we use a hash table, we must use ascii codes instead of characters.
Example-4:
Approach:
In this example, we first define a sample input string string as āhello worldā. Then, we define a translation table using the str.maketrans() method. The str.maketrans() method returns a translation table that can be used by the translate() method to replace characters in a string.
In this case, we define a translation table that replaces all occurrences of the characters āeā and ālā with āxā and āyā, respectively. We assign this translation table to the variable translation_table.
Finally, we use the translate() method to apply the translation table to the input string string. This returns a new string with the characters replaced according to the translation table. We assign this new string to the variable translated_string.
Python3
# Sample input string string = "hello world" Ā
# Define the translation table translation_table = str .maketrans( 'el' , 'xy' ) Ā
# Use the translate() method to apply the translation table to the input string translated_string = string.translate(translation_table) Ā
# Print the translated string print (translated_string) |
hxyyo woryd
The time complexity of this code is O(n), where n is the length of the input string. The str.maketrans() method has a constant time complexity as it simply creates a translation table, and the translate() method also has a linear time complexity as it loops through each character in the input string and performs the translation based on the table.
The space complexity is also O(n), as the translated string has the same length as the input string, and the translation table is a constant size regardless of the length of the input string.