Home » Python » Python String translate() method Tutorial

About Yatin

Working as a Technical Lead in the Information Technology sector where I'm primarily involved with projects based on Java/J2EE Technologies platform (such as Spring, Spring Boot, Hibernate, Redis, etc.), novel UI technologies (Angular & React), Docker, and AWS. Recently completed the AWS Solution Architect Associate Certification.

Python String translate() method Tutorial

Hello in this tutorial, we will understand how to use the String translate method in python programming.

1. Introduction

The translate() method in python programming is used to return a string in which all characters are translated using the translation table. The method is represented by the following syntax:

string.translate(translation_table)

where,
translation_table contains the mapping between two characters and is created by maketrans()
Python String translate

The method returns a string where each character is mapped to its corresponding character as per the given translation table.

  • If the character is not specified in the translation table then it will not be replaced
  • If you use a dictionary object then you must use ASCII codes instead of the characters

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String translate() method Tutorial

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice. Let us dive in with the programming stuff now.

2.1 Python String translate() method Tutorial

Let us understand the different use cases of the translate() method in python programming.

translate() method

# python translate() tutorial
# Returns a string where each character is mapped to its corresponding character
# in the translation table
# Syntax - string.translate(translation_table)
# where,
# translation_table contains the mapping between two characters and is created by maketrans()
# translation using maketrans() method
def translation(orig_string):
    print('Original string = {}'.format(orig_string))
    string1 = 'wxyz'
    string2 = 'efgh'
    translation_table = orig_string.maketrans(string1, string2)
    # translated string
    translated_string = orig_string.translate(translation_table)
    print('Translated string = {}'.format(translated_string))
# translation using manual translation
def manual_translation(orig_string):
    print('Original string = {}'.format(orig_string))
    # dictionary
    # http://sticksandstones.kstrom.com/appen.html
    translation_table = {117: None, 118: None, 119: None, 120: None, 121: 105}
    # translated string
    translated_string = orig_string.translate(translation_table)
    print('Translated string = {}'.format(translated_string))
# driver code
if __name__ == '__main__':
    random_string = 'uvwxyz'
    translation(random_string)
    print('\n')
    manual_translation(random_string)

If everything goes well the following output will be shown in the IDE console.

Console Output

Original string = uvwxyz
Translated string = uvefgh
Original string = uvwxyz
Translated string = iz

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • translate() method in python programming
  • Sample program to understand the translate() method use cases

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on translate() method in python programming.

Download
You can download the full source code of this example here: Python String translate() method Tutorial

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments