Wednesday, November 10, 2021

Python for Beginners: Translation table in Python

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.

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.

The post Translation table in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...