Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython String translate() method

Python String translate() method

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)


Output

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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments