How to Use maketrans() Function in Python

How to Use maketrans() Function in Python

4 mins read262 Views Comment
Updated on Mar 31, 2023 17:26 IST

Looking to learn about the maketrans() function in Python? This article provides a detailed guide on how to use this powerful function to translate and replace characters in your Python code.

2023_03_MicrosoftTeams-image-254.jpg

The Python maketrans() function produces a Dictionary-type item (a group of key-value pairs) that connects a string character (key) with its corresponding substitution (value), resulting in a character mapping that can be used during the string translation procedure. In this article, we will discuss this function and understand its use with the help of examples.

Introduction to maketrans() Function in Python

The maketrans() function is a built-in method in Python that is used to generate a mapping table to be used in string translations. This function creates a dictionary-like object that associates each character in a string with its corresponding replacement character.

The maketrans() function is often used in conjunction with the translate() method to replace specific characters in a string.

What is Programming What is Python
What is Data Science What is Machine Learning

Syntax of maketrans()

The syntax of maketrans() function in Python is given as:

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

Parameters of maketrans()

where x, y, and z are string arguments. x is the string to be replaced, y is the string to replace with, and z is a string containing characters that will be deleted from the input string.

Return Value of maketrans()

The maketrans() function returns a translation table that can be used by the translate() method to replace the specified characters in a string. The translate() method replaces each character in the input string that matches a key in the translation table with the corresponding value.

Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
3 months
– / –
2 weeks
– / –
16 weeks
β‚Ή1.7 K
3 months
– / –
– / –
β‚Ή4.24 K
2 weeks
β‚Ή3 K
3 weeks
– / –
4 months

Examples of Using the maketrans() Function in Python

Example 1: Translating Characters in a String

 
# Create a translation table
trans_table = str.maketrans("aeiou", "12345")
# Translate a string using the translation table
string = "This is a string"
translated_string = string.translate(trans_table)
print(translated_string)
Copy code

Output:

Th3s 3s 1 str3ng

In this example, we create a translation table that maps vowels to numbers. We then use the translate() method to replace the vowels in a given string with their corresponding numbers.

Must Check: Types of Modules in Python

Must Check: Working with Python datetime module 

Example 2: Deleting Characters from a String

 
# Create a translation table to delete vowels
trans_table = str.maketrans("", "", "aeiou")
# Translate a string using the translation table
string = "This is a string"
translated_string = string.translate(trans_table)
print(translated_string)
Copy code

Output:

Ths s strng

In this example, we create a translation table that deletes vowels from a given string. We do this by leaving the first two arguments of maketrans() empty and specifying the vowels to be deleted in the third argument. We then use the translate() method to delete the specified characters from the string.

Example 3: Mapping Unicode Characters

 
# Create a translation table to map Unicode characters
trans_table = str.maketrans({0x00E9: "e", 0x00FC: "u"})
# Translate a string using the translation table
string = "cafΓ© au lait"
translated_string = string.translate(trans_table)
print(translated_string)
Copy code

Output:

cafe au lait

In this example, we create a translation table that maps Unicode characters to their ASCII equivalents. We do this by passing a dictionary of Unicode characters and their replacements to maketrans(). We then use the translate() method to replace the specified characters in the string.

Conclusion

The maketrans() function provides a convenient way to create a mapping table for string translations in Python. Hope this article was helpful for you to understand how and why the maketrans() function is used in Python.

Happy Learning!!

Contributed By: Prerna Singh

Top Trending Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

FAQs

What is the purpose of the maketrans() function in Python?

The maketrans() function is used to generate a mapping table to be used in string translations. This function creates a dictionary-like object that associates each character in a string with its corresponding replacement character. The maketrans() function is often used in conjunction with the translate() method to replace specific characters in a string.

How do you use the maketrans() function in Python?

The maketrans() function is called as a method on a string object. It takes three string arguments: x, y, and z. x is the string to be replaced, y is the string to replace with, and z is a string containing characters that will be deleted from the input string. The function returns a translation table that can be used by the translate() method to replace the specified characters in a string.

What is a translation table in Python?

A translation table is a dictionary-like object that associates each character in a string with its corresponding replacement character. Translation tables are often used in conjunction with the translate() method to replace specific characters in a string.

What is the translate() method in Python?

The translate() method is a built-in method in Python that is used to replace specified characters in a string. It takes a translation table as an argument and returns a new string with the specified characters replaced.

What are some use cases for the maketrans() function in Python?

The maketrans() function is commonly used for tasks such as: u2022 Replacing characters in a string with their corresponding replacements u2022 Deleting specified characters from a string u2022 Mapping Unicode characters to their ASCII equivalents

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio