Saturday, September 21, 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

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?