How to Convert a String to a Dictionary in Python?

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.

Convert a String to a Dictionary in Python

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.

How to Convert a String to a Dictionary in Python

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.

Convert a String to a Dictionary in Python handle single quotes

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.