Python String translate() method
Last Updated :
04 Mar, 2023
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 = "geeksforgeeks"
print("Original string:", original_str)
# Altered string
print("Modified string:", original_str.translate(trans_dic))
Output:
Original string: geeksforgeeks
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 = "GeeksforGeeks"
trans = s.maketrans("G", "g")
print(s.translate(trans))
Output:
geeksforgeeks
Explanation:
Here 'G' is mapped with 'g' with maketrans() method.
Example 3:
Python3
# strings
str1 = "gfg"
str2 = "abc"
str3 = "gf"
original_string = "geeksforgeeks"
print("Initial string:", original_string)
translation = original_string.maketrans(str1, str2, str3)
# Altered String
print("Modified string:", original_string.translate(translation))
Output:
Initial string: geeksforgeeks
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)
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.
Similar Reads
Python String Title method title() method in Python is a simple way to convert a string to a title case, where the first letter of each word is capitalized and all other letters are lowercase. Let's understand with the help of an example:Pythona = "hello geek" # Converts string to title case b = a.title() print(b)OutputHello
2 min read
String Translate using Dict - Python In Python, translating a string based on a dictionary involves replacing characters or substrings in the string with corresponding values from a dictionary. For example, if we have a string "python" and a dictionary {"p": "1", "y": "2"}, we can translate the string to "12thon". Letâs explore a few m
3 min read
Python String Methods Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio
5 min read
Python - Sympy Curve.translate() method In Sympy, the function Curve.translate() is used translate the given curve by the given values of x, y. It translates the curve along with both the directions i.e. along x-axis and y-axis. Syntax: Curve.translate(x, y) Parameters: x: translation value along x-axis y: translation value along y-axis R
1 min read
Python Strings decode() method The decode() method in Python is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format. For example, let's try to encode and decode a simple text message:Pythons = "Geeks for Geeks" e =
4 min read
Python - Strings encode() method String encode() method in Python is used to convert a string into bytes using a specified encoding format. This method is beneficial when working with data that needs to be stored or transmitted in a specific encoding format, such as UTF-8, ASCII, or others.Let's start with a simple example to under
3 min read