In this tutorial, we will look at how to convert the string representation of a dictionary to a dictionary in Python.
How to turn a string into a dictionary in Python?

You can use the ast.literal_eval()
function to convert the string representation of a dictionary to a dictionary in Python. The following is the syntax.
import ast # convert string representation of dict to dict ast.literal_eval(s)
It returns a dictionary.
Let’s look at an example –
import ast # string representation of a dictionary s = '{"Name": "Clint Barton", "Age": 47, "Team": "Avengers", "Skills": "Archery"}' # convert s to dict d = ast.literal_eval(s) # display d and its type print(d) print(type(d))
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'} <class 'dict'>
We get the result as a dictionary.
Using json.loads()
Alternatively, you can use the json.loads()
function to convert a string representation of a dictionary to a dictionary in Python.
Let’s use this function on the above example –
import json # string representation of a dictionary s = '{"Name": "Clint Barton", "Age": 47, "Team": "Avengers", "Skills": "Archery"}' # convert s to dict d = json.loads(s) # display d and its type print(d) print(type(d))
Output:
{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'} <class 'dict'>
We get the same result as above, a dictionary.
Note that the json.loads()
function will only work on valid JSON strings. For example, if your string contains keys or values in single quotes, or if it has trailing commas, etc. this method will not work.
import json # string representation of a dictionary s = "{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'}" # convert s to dict d = json.loads(s) # display d and its type print(d) print(type(d))
Output:
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_7144/4188743168.py in <module> 4 s = "{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'}" 5 # convert s to dict ----> 6 d = json.loads(s) 7 # display d and its type 8 print(d) ... JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
We get a “JSONDecodeError”. This happened because the string s
here contains the keys and values in single quotes.
For more on JSON, refer to this guide.
But, if you use the ast.literal_eval()
function on the string above, it will correctly convert it to a dictionary.
import ast # string representation of a dictionary s = "{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'}" # convert s to dict d = ast.literal_eval(s) # display d and its type print(d) print(type(d))
Output:
{'Name': 'Clint Barton', 'Age': 47, 'Team': 'Avengers', 'Skills': 'Archery'} <class 'dict'>
We get the result without any errors with ast.literal_eval()
. Generally, using this method is more stable than json.loads()
for converting dictionary string to a dictionary in Python.
You might also be interested in –
- Python – Convert List to a String
- Convert Dictionary to List of Tuples in Python
- Convert Pandas Series to a Dictionary
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.