translate() returns a string that is modified string of givens string according to given translation mappings.
There are two ways to translate :
Parameters :
string.translate(mapping)
mapping– a dictionary having mapping between two characters.
Returns : Returns modified string where each character is mapped to its corresponding character according to the provided mapping table.
# Python3 code to demonstrate # translations without # maketrans() # specifying the mapping # using ASCII table = { 119 : 103, 121 : 102, 117 : None } # target string trg = "weeksyourweeks" # Printing original string print ("The string before translating is : ", end ="") print (trg) # using translate() to make translations. print ("The string after translating is : ", end ="") print (trg.translate(table)) |
The string before translating is : weeksyourweeks The string after translating is : geeksforgeeks
One more example:
# Python 3 Program to show working # of translate() method # specifying the mapping # using ASCII translation = {103: None, 101: None, 101: None} string = "geeks"print("Original string:", string) # translate string print("Translated string:", string.translate(translation)) |
Original string: geeks Translated string: ks
Syntax : maketrans(str1, str2, str3)
Parameters :
str1 : Specifies the list of characters that need to be replaced.
str2 : Specifies the list of characters with which the characters need to be replaced.
str3 : Specifies the list of characters that needs to be deleted.Returns : Returns the translation table which specifies the conversions that can be used by translate()
# Python 3 Program to show working # of translate() method # First String firstString = "gef" # Second String secondString = "eks" # Third String thirdString = "ge" # Original String string = "geeks"print("Original string:", string) translation = string.maketrans(firstString, secondString, thirdString) # Translated String print("Translated string:", string.translate(translation)) |
Original string: geeks Translated string: ks
Output :
Original string: geeks Translated string: ks
Recommended Posts:
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- numpy string operations | translate() function
- Python | maketrans() and translate() functions
- Python | Pandas Series.str.translate()
- Python - Sympy Curve.translate() method
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Merge Tuple String List values to String
- Python - Remove front K characters from each string in String List
- Python | Delimited String List to String Matrix
- Python | Sorting string using order defined by another string
- Python | Check if string ends with any string in given list
- Python - Length of shortest string in string list
- Python - Remove String from String List
- String slicing in Python to rotate a string
- Python | Sort each String in String list
- Python | Check if a given string is binary string or not
- String Alignment in Python f-string
- String to Int and Int to String in Python
- Python | Add one string to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

