Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to translate a python string according a given dictionary?
In Python, translating a string using a dictionary is commonly used for text manipulation and encoding. It is typically achieved by using the str.translate() method along with str.maketrans() to define character mappings.
The dictionary passed to maketrans() contains key-value pairs where the keys are characters to replace and values are their corresponding replacements. Let's explore different approaches to translate strings using dictionaries.
Using str.translate() with str.maketrans()
The str.translate() method uses a translation table generated by str.maketrans() to perform one-to-one character mapping efficiently.
Syntax
str.translate(table) str.maketrans(x, y, z)
Basic Character Replacement
Here's how to perform basic character replacement using a dictionary ?
translation_dict = {'a': '1', 'b': '2', 'c': '3'}
translation_table = str.maketrans(translation_dict)
demo = "abcab"
result = demo.translate(translation_table)
print(result)
12312
Removing Characters
You can remove characters by mapping them to None using the third parameter ?
translation_table = str.maketrans('', '', 'aeiou')
demo = "TutorialsPoint"
result = demo.translate(translation_table)
print(result)
TtrlsPnt
Case Conversion
Converting specific characters to uppercase using dictionary mapping ?
translation_dict = {'x': 'X', 'b': 'B', 'z': 'T'}
translation_table = str.maketrans(translation_dict)
text = "bxz zxb xbz"
result = text.translate(translation_table)
print(result)
BXT TXB XBT
Advanced Example: Reverse Alphabet Cipher
Creating a cipher where each letter maps to its reverse counterpart in the alphabet ?
import string
lowercase = string.ascii_lowercase
reverse_alphabet = lowercase[::-1]
translation_table = str.maketrans(lowercase, reverse_alphabet)
demo = "tutorialspoint"
result = demo.translate(translation_table)
print(f"Original: {demo}")
print(f"Encoded: {result}")
Original: tutorialspoint Encoded: gfglirzohklrmg
Alternative Method: Using Dictionary with replace()
For simple replacements, you can iterate through a dictionary and use replace() ?
def translate_with_dict(text, translation_dict):
for old_char, new_char in translation_dict.items():
text = text.replace(old_char, new_char)
return text
text = "hello world"
mapping = {'h': 'H', 'o': '0', 'l': '1'}
result = translate_with_dict(text, mapping)
print(result)
He110 w0r1d
Performance Comparison
| Method | Performance | Best For |
|---|---|---|
str.translate() |
Fast | Single character mappings |
replace() loop |
Slower | Simple, multi-character replacements |
Conclusion
Use str.translate() with str.maketrans() for efficient single-character translations. For multi-character replacements or simple cases, the replace() method provides a more readable alternative.
