
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to translate a python string according a given dictionary?
We can use the method translate() which returns a copy of the string in which all characters have been translated using table (constructed with the maketrans() function in the string module), optionally deleting all characters found in the string deletechars.
example
from string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "This is a string example....wow!!!"; print str.translate(trantab)
Output
This will give us the output −
Th3s 3s 1 str3ng 2x1mpl2....w4w!!!
- Related Articles
- How we can translate Python dictionary into C++?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- How to convert the string representation of a dictionary to a dictionary in python?
- How to convert a string to dictionary in Python?
- How to truncate a Python dictionary at a given length?
- How do I serialize a Python dictionary into a string, and then back to a dictionary?
- Program to find number of ways to form a target string given a dictionary in Python
- Python program to create a dictionary from a string
- How to print a value for a given key for Python dictionary?
- How to get a value for a given key from a Python dictionary?
- Convert string dictionary to dictionary in Python
- How to select Python Tuple/Dictionary Values for a given Index?
- How to define a Python dictionary within dictionary?
- How to check if a given key already exists in a Python dictionary?
- How do I format a string using a dictionary in Python 3?

Advertisements