In this Python string tutorial, you will learn how to convert string to dictionary in Python.
Method 1: eval() function
# Create a string S = "{'website': 'AiHints', 'pages': 7}" # Convert string into dictionary D = eval(S) # Print dictionary print(D) # Display the type of D print(type(D))
Output:
{'website': 'AiHints', 'pages': 7} <class 'dict'>
Method 2: json.loads() function
# Import json module import json # Create a string S = '{"website": "AiHints", "pages": 7}' # Convert string into dictionary D = json.loads(S) # Print dictionary print(D) # Display the type of D print(type(D))
Output:
{'website': 'AiHints', 'pages': 7} <class 'dict'>