Python | Convert dictionary object into string
Dictionary is an important container and used almost in every code of day-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.
Let’s see the different ways of changing dictionary into string.
Methods #1: Using json.dumps()
json.dumps() is an inbuilt function in json library.It has advantage over pickle because it has cross-platform support.
# Pyhton code to demonstrate # to conert dictionary into string # using json.dumps() import json # initialising dictionary test1 = { "testname" : "akshat", "test2name" : "manjeet", "test3name" : "nikhil"} # print original dictionary print (type(test1)) print ("initial dictionary = ", test1) # convert dictionary into string # using json.dumps() result = json.dumps(test1) # printing result as string print ("\n", type(result)) print ("final string = ", result) |
<class ‘dict’>
initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’}<class ‘str’>
final string = {“testname”: “akshat”, “test2name”: “manjeet”, “test3name”: “nikhil”}
Methods #2: Using str()
The str() function converts the specified value into a string.
# Pyhton code to demonstrate # to convert dictionary into string # using str() # initialising dictionary test1 = { "testname" : "akshat", "test2name" : "manjeet", "test3name" : "nikhil"} # print original dictionary print (type(test1)) print ("initial dictionary = ", test1) # convert dictionary into string # using str result = str(test1) # print resulting string print ("\n", type(result)) print ("final string = ", result) |
<class ‘dict’>
initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}<class ‘str’>
final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}
Recommended Posts:
- Python | Convert string dictionary to dictionary
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python | Ways to convert string to json object
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert a set into dictionary
- Python | Convert a list to dictionary
- Python | Convert two lists into a dictionary
- Python | Convert Tuples to Dictionary
- Python | Convert list of tuple into dictionary
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Convert list of nested dictionary into Pandas dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



