In programming, one often needs to perform string manipulations like replacing characters or deleting them from a string. In Python, there’s a built-in method for this purpose named str.maketrans().
This function returns a translation table by manually specifying all the replacement pairs of ASCII characters. This translation table can then be used with the translate() function to replace specified characters in the string.
Step 1: Understanding str.maketrans()
str.maketrans() is a static method that returns a translation table useful for mapping characters in strings. This function requires two arguments, both strings of equal length. Each character in the first string is mapped to the character at the same position in the second string.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Python code to demonstrate working of # str.maketrans() # specifying the mapping using ASCII table = str.maketrans("abc", "def") # Printing original string print ("The original string is : ", "abc") # Using str.translate() to make translations. print ("The string after translating is : ", end="") print("abc".translate(table)) |
The original string is : abc The string after translating is : def
Step 2: Deletion of Characters
This method can also be used to delete all the occurrence of certain characters.
1 2 3 4 5 6 7 8 9 10 11 12 |
# Python code to demonstrate working of str.maketrans() and deletions # specifying mappings # using bytes / bytearray maketrans() table = str.maketrans('', '', 'msp') # Printing original string print ("The string before translating is : ", "mapssm") # using str.translate() to make translations and deletions print ("The string after translating and deleting is : ", end="") print("mapssm".translate(table)) |
The string before translating is : mapssm The string after translating and deleting is : a
Step 3: Using str.maketrans() with translate()
In Python, str.translate() is a method that can be used to replace any part of the string. The new characters are provided by a translation table created by the maketrans method.
Here’s the entire code we used during the tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Python code to demonstrate working of str.maketrans() # specifying the mapping using ASCII table = str.maketrans("abc", "def") # Printing original string print ("The original string is : ", "abc") # Using str.translate() to make translations. print ("The string after translating is : ", end="") print("abc".translate(table)) #------------------ # Python code to demonstrate working of str.maketrans() and deletions # specifying mappings # using bytes / bytearray maketrans() table = str.maketrans('', '', 'msp') # Printing original string print ("The string before translating is : ", "mapssm") # using str.translate() to make translations and deletions print ("The string after translating and deleting is : ", end="") print("mapssm".translate(table)) |
Conclusion
Understanding how to use str.maketrans() and str.translate() in Python can greatly simplify the process of manipulating strings.
By creating a translation table, you can replace, map, or even delete characters, making this method particularly useful in data-cleaning activities.