Recently in a Python webinar, Someone asked me about how to convert a string to a dictionary in Python. Then I researched more about this topic and found two effective ways to accomplish this task. I will help you to learn more about this topic by providing suitable examples along with screenshots.
Convert a String to a Dictionary in Python
Imagine you have a string that represents a dictionary, such as:
employee_data = "{'name': 'John Smith', 'age': 35, 'city': 'New York'}"Your goal is to convert this string into a dictionary object so that you can easily access and manipulate its key-value pairs.
Read How to Convert Bytes to Strings in Python?
Method 1: Use ast.literal_eval() Method
One of the safest ways to convert a string to a dictionary is by using the ast.literal_eval() function from the ast module. This function evaluates a string as a Python literal structure, including dictionaries. Here’s an example:
import ast
employee_data = "{'name': 'John Smith', 'age': 35, 'city': 'New York'}"
employee_dict = ast.literal_eval(employee_data)
print(employee_dict) Output:
{'name': 'John Smith', 'age': 35, 'city': 'New York'}I have executed the above example code and added the screenshot below.

The ast.literal_eval() function is recommended over the eval() function because it is more secure and only evaluates Python literal structures, reducing the risk of executing malicious code.
Check out How to Split a String into Characters in Python?
Method 2: Use json.loads() Method
Another popular method to convert a string to a dictionary in Python is by using the json.loads() function from the json module. This function parses a JSON-formatted string and returns a dictionary object. Here’s an example:
import json
customer_data = '{"name": "Emily Johnson", "age": 28, "city": "Los Angeles"}'
customer_dict = json.loads(customer_data)
print(customer_dict)Output:
{'name': 'Emily Johnson', 'age': 28, 'city': 'Los Angeles'}I have executed the above example code and added the screenshot below.

The json.loads() function is particularly useful when working with JSON data, which is a common format for data exchange in web applications.
Read How to Find a Character in a String Using Python?
Handle Single Quotes
If your string uses single quotes for the dictionary representation, you’ll need to replace them with double quotes before using json.loads(). Here’s an example:
import json
product_data = "{'name': 'iPhone 13', 'price': 999, 'brand': 'Apple'}"
product_data = product_data.replace("'", '"')
product_dict = json.loads(product_data)
print(product_dict)Output:
{'name': 'iPhone 13', 'price': 999, 'brand': 'Apple'}I have executed the above example code and added the screenshot below.

Check out How to Convert a String to Lowercase in Python?
Conclusion
In this tutorial, I have helped you to understand how to convert a string to a dictionary in Python. I have discussed methods like ast.literal_eval method, and json.loads method. I also discussed how to handle single quotes.
You may also like to read:
- How to Concatenate String and Int in Python?
- How to Remove Punctuation from Strings in Python?
- How to Swap Commas and Dots in a String in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.