Python | Convert dictionary object into string
Dictionary is an important container and used almost in every code of day-to-day programming as well as web-development. The more it is used, more is the requirement to master it and hence its necessary to learn about them.
Let’s see the different ways of changing a dictionary into a 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.
Python3
# Python code to demonstrate # to convert 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) |
Output:
initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’} final string = {“testname”: “akshat”, “test2name”: “manjeet”, “test3name”: “nikhil”}
Methods #2: Using str()
The str() function converts the specified value into a string.
Python3
# Python 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) |
Output:
initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}
Methods #3: Using pprint
Another approach to convert a dictionary object into a string is to use the pprint module. The pprint module provides a way to pretty-print arbitrary Python data structures in a form which can be used as input to the interpreter.
Here is an example of using the pprint module to convert a dictionary object into a string:
Python3
import pprint # Initialize dictionary d = { "testname" : "akshat" , "test2name" : "manjeet" , "test3name" : "nikhil" } # Print original dictionary print (f "Original dictionary: {d}" ) # Convert dictionary into string using pprint.pformat() result = pprint.pformat(d) # Print resulting string print (f "\nResulting string: {result}" ) print ( "Type is: " , type (result)) #This code is contributed by Edula Vinay Kumar Reddy |
Original dictionary: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Resulting string: {'test2name': 'manjeet', 'test3name': 'nikhil', 'testname': 'akshat'} Type is: <class 'str'>
The pprint module provides more control over the formatting of the resulting string, such as indentation and line width, than the built-in str and json.dumps functions.
Please Login to comment...