Open In App

Python | Convert dictionary object into string

Improve
Improve
Like Article
Like
Save
Share
Report

The dictionary is an important container and is used almost in every code of day-to-day programming as well as web development with Python. The more it is used, the more is the requirement to master it and hence it’s necessary to learn about them. 

Input: { "testname" : "akshat","test2name" : "manjeet","test3name" : "nikhil"}
Output: {"testname": "akshat", "test2name": "manjeet", "test3name": "nikhil"}
Explanation: Input type is <class 'dict'> but the output type is <class 'str'>

Let’s see the different ways of changing a dictionary into a string.

Dictionary object into string Conversation

Below are the methods that we will cover in this article:

Converting Dict to String in Python using json.dumps() method

Here we can use the dump() method from the JSON library by importing it which converts the dictionary data type to string. In the below code, we first take a dictionary test1 then we use json.dumps method and pass the tes1 dictionary in it and we will get the required result in the string format.

Python3




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
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”}

Space complexity : O(n)
Time complexity : O(n)

Dictionary into string conversion using str() function

The str() function converts the specified value into a string. The string function is also helpful to convert the data type into string type by this we pass the dictionary into this method and it will convert the datatype form dictionary to string datatype.

Python3




test1 = { "testname" : "akshat",
          "test2name" : "manjeet",
          "test3name" : "nikhil"}
 
# print original dictionary
print (type(test1))
print ("initial dictionary = ", test1)
 
# convert dictionary into string
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’}

Space complexity : O(n)
Time complexity : O(n)

Convert dictionary into string using print method

Another approach to convert a dictionary object into a string is to use the print. The print provides a way to pretty-print arbitrary Python data structures in a form that print can be used as input to the interpreter.

Here is an example of using the print that 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))


Output

Original dictionary: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'}

Resulting string: {'test2name': 'manjeet', 'test3name': 'nikhil', 'testname': 'akshat'}
Type is:  <class 'st...

Space complexity : O(n)
Time complexity : O(n)

The print 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.



Last Updated : 04 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads