Python String maketrans() Method – Tutorial with Examples

Python String maketrans() Method

Python provides various string methods to perform operations on strings. One of them is the maketrans() method. The maketrans() method is used to create a translation table to be used by the translate() method.

Syntax

The syntax for using the maketrans() method is as follows:

str.maketrans(x[, y[, z]])

Here’s what the parameters do:

  • x – If only one argument is supplied, it must be a dictionary mapping Unicode ordinals to translation strings. If two arguments are passed, it must be two strings with equal length. Each character in the first string is a replacement to its corresponding index in the second string. If three arguments are passed, each character in the third argument is mapped to None.
  • y – The second parameter, if present, must be a string of characters that will be mapped to None.
  • z – The third parameter, if present, specifies a string of characters that will be mapped to each other.

The maketrans() method returns a translation table as a dictionary, which can be used by the translate() method to perform translations on strings.

Examples

Here are some examples to illustrate the usage of the maketrans() method:

Example 1: Creating a translation table to replace characters

# Create a translation table to replace "l" with "1" and "o" with "0"
table = str.maketrans("lo", "10")
# Use the translation table to translate a string
string = "hello world"
translated = string.translate(table)
print(translated)

Output:

h3110 w0rld

The maketrans() method is used to create a translation table that maps “l” to “1” and “o” to “0”. The resulting table is used with the translate() method to replace the characters “l” and “o” in the string “hello world” with “1” and “0”, respectively.

Example 2: Creating a translation table to remove characters

# Create a translation table to remove vowels
table = str.maketrans("", "", "aeiou")
# Use the translation table to translate a string
string = "the quick brown fox jumps over the lazy dog"
translated = string.translate(table)
print(translated)

Output:

th qck brwn fx jmps vr th lzy dg

The maketrans() method is used to create a translation table that maps vowels to None. The resulting table is used with the translate() method to remove all vowels from the string “the quick brown fox jumps over the lazy dog”.

Example 3: Creating a translation table with a dictionary

# Create a translation table with a dictionary to replace "cat" with "dog" and "dog" with "cat"
table = str.maketrans({"cat": "dog", "dog": "cat"})
# Use the translation table to translate a string
string = "The cat and the dog are friends"
translated = string.translate(table)
print(translated)

Output:

The dog and the cat are friends

The maketrans() method is used to create a translation table with a dictionary that maps “cat” to “dog” and “dog” to “cat”. The resulting table is used with the translate() method to replace all occurrences of “cat” with “dog” and vice versa in the string “The cat and the dog are friends”.

Use Cases

The maketrans() method is useful when performing string operations that involve character replacements or removal. Some use cases include:

  • Replacing certain characters in a string with other characters
  • Removing certain characters from a string
  • Swapping one set of characters with another set of characters

The maketrans() method can be combined with the translate() method to perform these operations efficiently and with greater flexibility.

Leave a Reply

Your email address will not be published. Required fields are marked *