Converting String Content to Dictionary - Python Last Updated : 22 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, a common requirement is to convert string content into a dictionary. For example, consider the string "{'a': 1, 'b': 2, 'c': 3}". The task is to convert this string into a dictionary like {'a': 1, 'b': 2, 'c': 3}. This article demonstrates several ways to achieve this in Python.Using ast.literal_evalThis method is one of the most efficient and safe ways to convert a string representation of a dictionary into an actual dictionary. Python import ast # Input string s = "{'a': 1, 'b': 2, 'c': 3}" # Convert string to dictionary using ast.literal_eval result = ast.literal_eval(s) print(result) Output{'a': 1, 'b': 2, 'c': 3} Explanation:ast.literal_eval function safely evaluates the string as a Python literal.This method is preferred because it avoids the risks associated with using eval().Using json.loadsIf the string uses double quotes instead of single quotes, you can use the json.loads method from the json module. Python import json # Input string s = '{"a": 1, "b": 2, "c": 3}' # Convert string to dictionary using json.loads res = json.loads(s) print(res) Output{'a': 1, 'b': 2, 'c': 3} Explanation:json.loads function parses the string as a JSON object and converts it to a Python dictionary.Ensure the input string follows JSON formatting (e.g., double quotes for keys and values).Using eval()eval() function can also be used for this purpose but should be avoided in production code due to potential security risks. Python # Input string s = "{'a': 1, 'b': 2, 'c': 3}" # Convert string to dictionary using eval res = eval(s) print(res) Output{'a': 1, 'b': 2, 'c': 3} Explanation:eval() function evaluates the string as a Python expression.While this method works, it can execute arbitrary code, making it unsafe for untrusted input.Using String Manipulation and splitFor simple strings with a fixed pattern, you can manually parse the string. Python # Input string s = "{'a': 1, 'b': 2, 'c': 3}" # Remove curly braces and split the string pairs = s.strip('{}').split(', ') res = {pair.split(': ')[0].strip("'"): int(pair.split(': ')[1]) for pair in pairs} print(res) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is stripped of curly braces using strip('{}').The string is split into key-value pairs using split(', ').Dictionary comprehension is used to create the dictionary by splitting and processing each pair. Comment More infoAdvertise with us Next Article Converting String Content to Dictionary - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Python - Convert Dictionary to Concatenated String In Python, sometimes we need to convert a dictionary into a concatenated string. The keys and values of the dictionary are combined in a specific format to produce the desired string output. For example, given the dictionary {'a': 1, 'b': 2, 'c': 3}, we may want the string "a:1, b:2, c:3". Let's dis 3 min read Convert String Dictionary to Dictionary in Python The goal here is to convert a string that represents a dictionary into an actual Python dictionary object. For example, you might have a string like "{'a': 1, 'b': 2}" and want to convert it into the Python dictionary {'a': 1, 'b': 2}. Let's understand the different methods to do this efficiently.Us 2 min read Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st 3 min read Splitting the String and Converting it to Dictionary In Python, we may need to convert a formatted string into a dictionary. For example, given the string "a:1, b:2, c:3", we want to split the string by its delimiters and convert it into a dictionary. Let's explore various methods to accomplish this task.Using Dictionary ComprehensionWe can split the 3 min read Python - Concatenate Dictionary string values The task of concatenating string values in a Python dictionary involves combining the values associated with the same key across multiple dictionaries. This operation allows us to merge string data in an efficient manner, especially when dealing with large datasets .For example, given two dictionari 4 min read Like