How to translate a python string according a given dictionary?


To translate a Python string according to a given dictionary, you can use the translate() method in combination with str.maketrans() function. The str.maketrans() function creates a translation table from a given dictionary that maps each character in the dictionary to its corresponding translation. Here are some examples:

Translate a string using a dictionary

Example

The dictionary translation_dict is defined to map each letter in the input string to its corresponding digit. We create a translation table using str.maketrans() function and apply it to the sample string text using the translate() method. The result is the translated string, which replaces each letter with its corresponding digit.

# define a dictionary to map characters to their translations
translation_dict = {"a": "1", "b": "2", "c": "3", "d": "4"}
# define a sample string to be translated
text = "abcd"
# create a translation table using the dictionary
translation_table = str.maketrans(translation_dict)
# translate the string using the table
translated_text = text.translate(translation_table)
# print the translated string
print(translated_text)

Output

1234

Translate a string using a dictionary with some missing keys

Example

In this example, the dictionary translation_dict does not contain a translation for the letter "d" and "e". When the translate() method encounters these characters, it simply leaves them unchanged in the translated string.

# define a dictionary to map characters to their translations
translation_dict = {"a": "1", "b": "2", "c": "3"}
# define a sample string to be translated
text = "abcde"
# create a translation table using the dictionary
translation_table = str.maketrans(translation_dict)
# translate the string using the table
translated_text = text.translate(translation_table)
# print the translated string
print(translated_text)

Output

123de

Translate a string using a dictionary with additional characters

Example

In this example, the dictionary translation_dict contains translations not only for letters but also for spaces and periods. The resulting translation replaces each character with its corresponding value in the dictionary, including the space and period, which are translated to "-" and "!" respectively.

# define a dictionary to map characters to their translations
translation_dict = {"a": "1", "b": "2", "c": "3", "d": "4", " ": "-", ".": "!"}
# define a sample string to be translated
text = "a b.c.d"
# create a translation table using the dictionary
translation_table = str.maketrans(translation_dict)


# translate the string using the table
translated_text = text.translate(translation_table)
# print the translated string
print(translated_text)

Output

 1-2!3!4

Using the str.translate() method with a dictionary

Example

In this example, we first create a dictionary to define the character translations. We then create a translation table using the str.maketrans() method and pass in the dictionary as an argument. We then use the translate() method with the translation table to translate the string. The output should be "56789".

# create a dictionary to translate characters
translation_dict = {
    "a": "5",
    "b": "6",
    "c": "7",
    "d": "8",
    "e": "9"
}
# sample string
text = "abcde"
# create a translation table from the dictionary
translation_table = str.maketrans(translation_dict)
# translate the string using the translation table
translated_text = text.translate(translation_table)
# print the translated text
print(translated_text)

Output

56789

Using the str.replace() method with a loop

Example

In this example, we use a loop to iterate through the dictionary and replace each character in the string. We use the str.replace() method to replace each occurrence of the key in the dictionary with the corresponding value. The output should be "12345". However, this method may not be as efficient as the previous examples for larger strings and dictionaries.

# create a dictionary to translate characters
translation_dict = {
    "a": "1",
    "b": "2",
    "c": "3",
    "d": "4",
    "e": "5"
}
# sample string
text = "abcde"
# loop through the dictionary and replace each character in the string
for key, value in translation_dict.items():
    text = text.replace(key, value)
# print the translated text
print(text)

Output

12345

Updated on: 11-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements