close

How to convert dictionary to string in python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to convert dictionary to string in python. We will learn the conversion of a dictionary into string in python. There are some method to convert dict into string. Here we will discuss about of that. So without wasting your time lets learn about of this by given below methods.

How to convert dictionary to string in python

  1. convert dictionary to string in python

    to convert dictionary to string in python just Use json(). json() function used for to convert the dictionary into a string. The json.dumps() take a dictionary as a parameter and the return the value in string. So first of all you have to import json and then make your dict and print your dict with the function json() and it will give you the string. Lets learn this by given below example: import json dict = {'fruit':45} var1 = json.dumps(dict) print(var1) var2 = json.loads(var1) print(var2) Output : {"fruit": 45} {'fruit': 45}

  2. How to convert dictionary to string in python

    to convert dictionary to string in python just Use literal_eval(). This method can only be used when the dictionary is a small. For bif dictionary you cannot use this method. By using literal_eval() you can convert your dictionary into string. Lets learn this by given below example: import ast mydict = {'fruit':45} str(mydict) d = ast.literal_eval(str(mydict)) print(d) Output : {'fruit': 45}

Method 1: Use json()

json() function used for to convert the dictionary into a string. The json.dumps() take a dictionary as a parameter and the return the value in string. So first of all you have to import json and then make your dict and print your dict with the function json() and it will give you the string. Lets learn this by given below example:

import json
dict = {'fruit':45}
var1 = json.dumps(dict)
print(var1)
var2 = json.loads(var1)
print(var2)

Output :

{"fruit": 45}
{'fruit': 45}

Method 2: Use literal_eval()

This method can only be used when the dictionary is a small. For bif dictionary you cannot use this method. By using literal_eval() you can convert your dictionary into string. Lets learn this by given below example:

import ast
mydict = {'fruit':45}
str(mydict)
d = ast.literal_eval(str(mydict))
print(d)

Output :

{'fruit': 45}

Method 3: Use pickle

Here we will use pickle for to convert the dictionary into the string. You can use dumps with pickle and it will give you the string which is converted from dictionary. You can also convert this string into dictionary by using loads with pickle. Thus it is the opposite of the first one. To use this method first of all import pickle. and then use it with dumps() function. It will give your string. And then use loads() function. It will convert the string into dictionary. Lets learn this by given below example::

import pickle
dict = {'fruit':45}
var1 = pickle.dumps(dict)
print(var1)
var2 = pickle.loads(var1)
print(var2)

Output :

b'\x80\x04\x95\x0e\x00\x00\x00\x00\x00\x00\x00}\x94\x8c\x05fruit\x94K-s.'
{'fruit': 45}

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment