• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Translation table in Python

Translation table in Python

Author: Aditya Raj
Last Updated: November 10, 2021

Python provides us with different ways with which we can manipulate strings. In this article, we will discuss the translation table and will use it to replace a character with another character in a string in python.

Table of Contents
  1. What is a translation table?
  2. How to create a translation table?
  3. How to use the translation table?
  4. Conclusion

What is a translation table?

In simple terms, a translation table is a mapping of one character to another. While working with strings, we may need to replace a character with another character in a string. In such cases, we can use a translation table to determine which character has to be replaced with which character.

You can think of the translation table as a dictionary in which the keys are the characters originally present in a string and values are the characters with which the existing characters will be replaced.

Now, let us see how we can create a translation table in python.

How to create a translation table?

Python provides us with the maketrans() function with which we can create a translation table. The maketrans() function takes three arguments and returns a translation table. The first argument is the string containing the characters that need to be replaced. The second  input argument is the string containing new characters. The third and optional argument is a string containing characters that need to be deleted from any string.  The syntax for the maketrans() function is as follows:

maketrans(old_characters,new_characters,characters_to_delete). Here,

  • old_characters  is the string containing the characters that need to be replaced. 
  • new_characters is the string containing the characters that will be used in place of the characters in old_characters. Ideally, the length of new_characters should be equal to old_characters. In this way, each character in old_characters will be mapped to character at the corresponding location in the new_character. 
  • characters_to_delete  contains the characters that will be deleted from any string.

We can create a translation table in python as shown in the example given below. 

import string
input_string = """This is PythonForBeginners.com.
Here, you   can read python tutorials for free."""
translation_table = input_string.maketrans("abcdefghijklmnopqrstupwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
print("The translation table is:")
print(translation_table)

Output:

The translation table is:
{97: 65, 98: 66, 99: 67, 100: 68, 101: 69, 102: 70, 103: 71, 104: 72, 105: 73, 106: 74, 107: 75, 108: 76, 109: 77, 110: 78, 111: 79, 112: 86, 113: 81, 114: 82, 115: 83, 116: 84, 117: 85, 119: 87, 120: 88, 121: 89, 122: 90}

Here, the ASCII values of characters A to Z and a to z are used to created the translation table. The keys of the translation table are the ASCII values of lowercase alphabets and the corresponding values are ASCII values of uppercase alphabets. We can use this translation table to replace a lowercase character with its uppercase counterpart. We have not specified any character that has to be deleted.

How to use the translation table?

We use the translation table with the translate() method to replace characters in a string. The translate() method, when invoked on a string, takes the translation table as its input and replaces the characters in the original string according to the translation table. You can understand this from the following example.

import string
input_string = """This is PythonForBeginners.com.
Here, you   can read python tutorials for free."""
translation_table = input_string.maketrans("abcdefghijklmnopqrstupwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
new_string = input_string.translate(translation_table)
print("The original string is:")
print(input_string)
print("Output String is:")
print(new_string)

Output:

The original string is:
This is PythonForBeginners.com.
Here, you   can read python tutorials for free.
Output String is:
THIS IS PYTHONFORBEGINNERS.COM.
HERE, YOU   CAN READ VYTHON TUTORIALS FOR FREE.

Here, we have replaced all the lowercase characters with their uppercase counterparts using the translation table created in the previous example.

Conclusion

In this article, we have discussed the translation table in python. We also saw how to use the maketrans() method and the translate() method to replace characters in a string.  To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us