by @codeexamples

Converting string to dictionary in Python

Home » python » Converting string to dictionary in Python

To convert a string to dictionary we can the built-in ast.literal_eval() method in Python.

Here is an example:

import ast

str = "{'bat' : 'rat', 'mat' : 'kitty'}"

print(ast.literal_eval(str))

Output:

{'bat': 'rat', 'mat': 'kitty'}