Python has many data structures to work with, and each structure adds something to the table. Often we need to convert from one data structure to another to pass the data seamlessly.
Python Dictionary is a required container used in every code of day-to-day programming and web development. The more it is used, the more the requirement to master it; hence, knowledge of its operations is necessary.
Python dict to string
To convert a dictionary to string in Python, use the json.dumps() function. The json.dumps() is a built-in function that converts an object into a json string.
# app.py import json stranger = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} # print original dictionary print("initial dictionary = ", stranger) print(type(stranger)) # convert dictionary into string # using json.dumps() op = json.dumps(stranger) # printing result as string print("final string = ", op) print("\n", type(op))
Output
➜ pyt python3 app.py initial dictionary = {'Eleven': 'Millie', 'Mike': 'Finn', 'Will': 'Noah'} <class 'dict'> final string = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} <class 'str'> ➜ pyt
First, we must print the original dictionary and its type in the above code. Then, we used json.dumps() function to convert a dictionary to a string, and then we print the string and its data type.
Python dict to string using str() function
Python str() is a built-in function that converts the specified value into a string.
# app.py stranger = {"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"} # print original dictionary print("initial dictionary = ", stranger) print(type(stranger)) # convert dictionary into string # using str() op = str(stranger) # printing result as string print("final string = ", op) print("\n", type(op))
Output
➜ pyt python3 app.py initial dictionary = {'Eleven': 'Millie', 'Mike': 'Finn', 'Will': 'Noah'} <class 'dict'> final string = {'Eleven': 'Millie', 'Mike': 'Finn', 'Will': 'Noah'} <class 'str'> ➜ pyt
First, we printed an original dictionary and its type in the above code. Then, we used the str() function to convert the dictionary to a string.
Python string to dictionary
To convert a string to a dictionary in Python, use the ast module’s literal.eval() function. The ast.literal_eval() is a ast library method that evaluates strings containing values from unknown sources without us having to parse the values.
import ast stranger = '{"name": "Krunal", "age": "26"}' # print original string print("initial string = ", stranger) print(type(stranger)) # convert string into dictionary # using ast.literal_eval() op = ast.literal_eval(stranger) # printing result as string print("final dictionary = ", op) print("\n", type(op))
Output
➜ pyt python3 app.py initial string = {"name": "Krunal", "age": "26"} <class 'str'> final dictionary = {'name': 'Krunal', 'age': '26'} <class 'dict'> ➜ pyt
First, we printed the original string and its type and then used the ast.literal_eval() function to convert the string to a dictionary, and then we printed dictionary and its type.
That’s it for this tutorial.