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. so without wasting time lets learn about of this.
How to convert dictionary to string in python
- convert dictionary to string in python
to convert dictionary to string in python just use str() By using str() you can convert your dictionary to string in python. So lets learn about of this without wasting time.
dict1 = {"happy": "burger", "mital": "pizza", "ridhi": "dhosa"} print("my dictionary = ", dict1) print(type(dict1)) new1 = str(dict1) print("my string = ", new1) print(type(new1))
Output :my dictionary = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'} <class 'dict'> my string = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'} <class 'str'>
- How to convert dictionary to string in python
to convert dictionary to string in python just use json.dumps() By using json.dumps() you can convert your dictionary to string in python. So lets learn about of this without wasting time.
import json dict1 = {"happy": "burger", "mital": "pizza", "ridhi": "dhosa"} print("my dictionary = ", dict1) print(type(dict1)) new1 = json.dumps(dict1) print("my string = ", new1) print(type(new1))
Output :my dictionary = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'} <class 'dict'> my string = {"happy": "burger", "mital": "pizza", "ridhi": "dhosa"} <class 'str'>
Method 1: Using str()
By using str() you can convert your dictionary to string. So lets learn about of this without wasting time.
dict1 = {"happy": "burger",
"mital": "pizza",
"ridhi": "dhosa"}
print("my dictionary = ", dict1)
print(type(dict1))
new1 = str(dict1)
print("my string = ", new1)
print(type(new1))
Output :
my dictionary = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'}
<class 'dict'>
my string = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'}
<class 'str'>
Download Code
Download Above code as file and Run In Your System Just download above code as File Here.
Method 2: Using json.dumps
By using json.dumps() you can convert your dictionary. So lets learn about of this without wasting time.
import json
dict1 = {"happy": "burger",
"mital": "pizza",
"ridhi": "dhosa"}
print("my dictionary = ", dict1)
print(type(dict1))
new1 = json.dumps(dict1)
print("my string = ", new1)
print(type(new1))
Output :
my dictionary = {'happy': 'burger', 'mital': 'pizza', 'ridhi': 'dhosa'}
<class 'dict'>
my string = {"happy": "burger", "mital": "pizza", "ridhi": "dhosa"}
<class 'str'>
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read