AppDividend
Latest Code Tutorials

How To Convert Python Dictionary To String Example

0

Python has so 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 and used almost in every code of day to day programming as well as web development. More it is used, more is the requirement to master it, and hence, knowledge of its operations is necessary.

Python Dictionary to String

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 at some point during a program execution and loaded back into the dictionary object when the program is rerun.

How can I convert a dictionary object into a string that can be 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 using json.dumps() function

The json.dumps() is an inbuilt function in json library.

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

In the above code, first, we have print the original dictionary and its type.

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

In the above code, first, we have print the original dictionary and its type.

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 that can 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

In the above code, first, we have print the original string and its type.

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 str() method of Python.

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

Leave A Reply

Your email address will not be published.

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