Python

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

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
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
Back to top button