AppDividend
Latest Code Tutorials

Python dict to string: How to Convert Dictionary to String

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. In this tutorial, we will see How To Convert Python Dictionary To String.

We have already seen the following conversion.

  1. Python string to int
  2. Python string to list
  3. Python list to dict
  4. Python dict to json

Python Dictionary is a vital container used almost in every code of day-to-day programming and web development. The more it is used, the more is the requirement to master it, and hence, knowledge of its operations is necessary.

Let’s consider the following scenario.

I am writing the program that stores data in a dictionary object. Still, this data needs to be saved during program execution and loaded back into the dictionary object when the program is rerun.

How can I convert a dictionary object into a string written to a file and loaded back into the dictionary object? This will hopefully support dictionaries containing dictionaries.

Let’s check the first method to convert dict to string in Python.

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 in json library that can be used by importing the json module into the head of the program.

It has an advantage over pickle because it has cross-platform support.

See the following code.

# 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 have to print the original dictionary and its type in the above code.

Then we have used the Python json.dumps() function to convert the dictionary to string, and then we print the string and its type.

Python dict to string using str() function

Python str() function converts the specified value into a string.

See the following code.

# 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 have to print the original dictionary and its type in the above code.

Then we have used the Python str() function to convert the dictionary to string, and then we print the string and its type.

Python String to Dictionary

We have seen two ways to convert the Python dictionary to string, but now we will see how to convert Python dictionary to string using Python ast module’s literal.eval() function.

See the following code.

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 have to print the original string and its type in the above code.

Then we have used the Python ast.literal_eval() function to convert the string to a dictionary, and then we print the dictionary and its type.

Summary

If you are asking to convert the Dictionary to a String then, How about using the str() method of Python.

If you ask to convert a String to a Dictionary, How about using ast.literal.eval() method of Python.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.