String Translate using Dict - Python Last Updated : 04 Feb, 2025 Comments Improve Suggest changes Like Article Like Report In Python, translating a string based on a dictionary involves replacing characters or substrings in the string with corresponding values from a dictionary. For example, if we have a string "python" and a dictionary {"p": "1", "y": "2"}, we can translate the string to "12thon". Let’s explore a few methods to translate strings using dictionaries.Using str.translate()translate() method in Python allows for efficient string translation by using a translation table created from a dictionary. This method works well when replacing characters in a string. Python # Input string and dictionary a = "python" b = {"p": "1", "y": "2"} # Creating translation table from dictionary trans = str.maketrans(b) # Translating the string using the table c = a.translate(trans) print(c) Output12thon Explanation:The str.translate() method converts the dictionary into a translation table.The translate() method uses this table to replace characters in the string.This method is very efficient for character replacements.Using loop and str.replace()If we need to replace multiple substrings in a string based on a dictionary, we can use a for loop and the replace() method for each key-value pair in the dictionary. Python # Input string and dictionary a = "python" b = {"p": "1", "y": "2"} # Looping through the dictionary to replace characters for k, v in b.items(): a = a.replace(k, v) print(a) Output12thon Explanation:We loop through each key-value pair in the dictionary.For each pair, the replace() method is called on the string to replace occurrences of the key with the corresponding value.This method is simple but less efficient than translate() for large strings or many replacements.Using List ComprehensionList comprehension can be used to iterate through the string and apply the dictionary mappings to each character. This method is similar to using a loop but more compact. Python # Input string and dictionary a = "python" b = {"p": "1", "y": "2"} # Using list comprehension to translate string c = ''.join([b.get(i, i) for i in a]) print(c) Output12thon Explanation:List comprehension iterates over each character in the string.The get() method is used to check if a character exists in the dictionary. If it does, it replaces it; otherwise, it keeps the character as is.Using reduce()The reduce() function from the functools module can also be used to apply a dictionary mapping to a string. It is more functional and can be used when we need to accumulate the result of applying the dictionary's values to each character. Python from functools import reduce # Input string and dictionary a = "python" b = {"p": "1", "y": "2"} # Using reduce to apply replacements c = reduce(lambda x, y: x.replace(y, b.get(y, y)), b.keys(), a) print(c) Output12thon Explanation:The reduce() function applies the replace() method to the string for each key in the dictionary.The get() method ensures that characters not in the dictionary are not replaced. Comment More infoAdvertise with us Next Article String Translate using Dict - Python K khushidg6jy Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads Working with Strings in Python 3 In Python, sequences of characters are referred to as Strings. It used in Python to record text information, such as names. Python strings are "immutable" which means they cannot be changed after they are created.Creating a StringStrings can be created using single quotes, double quotes, or even tri 5 min read Convert Unicode String to Dictionary in Python Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert 2 min read Python String A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1.Pythons = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # update print(s1) # printOut 6 min read Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr 1 min read Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain 2 min read Python | Set 3 (Strings, Lists, Tuples, Iterations) In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo 3 min read Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val 3 min read Collections.UserString in Python Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example: Python3 # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World' 2 min read Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s 3 min read Few mistakes when using Python dictionary Usually, A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. Each key-value pair in a Dictionary is separated by a 'colon', whereas each key is separated by a âcommaâ. Python3 1== my_dict = { 3 min read Like