How to Convert Dictionary to a String in Python?

Dictionary in Python:

A dictionary is a Python object that stores data in the key:value format. A colon(:) separates the key and its corresponding value. A comma (,) separates each key:value pair in the dictionary.  In Python, a dictionary is always enclosed in curly brackets.

A Python dictionary, unlike a list or a tuple, is an unordered data structure. We can directly access any value in a Python dictionary by using the key that corresponds to it.

Converting Dictionary to a String in Python

In Python, there are various methods for converting a dictionary to a string.

The most common methods are as follows:

Method #1: Using str() Function

The str() method is a very simple method to convert a dictionary to a string. The resulting string contains all of the dictionary’s keys, values, colon, and even curly braces.

Syntax:

str(dict)

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Print the given original dictionary
  • Print the datatype of the given original dictionary using the type() function
  • Convert the given dictionary to string using the str() function by passing the given dictionary as an argument to it.
  • Store it in another variable.
  • Print the given dictionary after conversion to string.
  • Print the datatype of the given dictionary after conversion using the type() function.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {"hello": 10 ,"Python": 20 ,"programs": 30}
# Print the given original dictionary
print("The given original dictionary = ", gvn_dict)
# Print the datatype of the given original dictionary using the type() function
print("The datatype of the given original dictionary = ", type(gvn_dict))
print()

# Convert the given dictionary to string using the str() function by passing 
# given dictionary as an argument to it.
# Store it in another variable.
str_conversion =str(gvn_dict)
# Print the given dictionary after conversion to string.
print("The given dictionary after conversion to string = ", str_conversion)
# Print the datatype of the given dictionary after conversion using the type() function
print("The datatype of the given dictionary after conversion = ", type(str_conversion))

Output:

The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}
The datatype of the given original dictionary = <class 'dict'>

The given dictionary after conversion to string = {'hello': 10, 'Python': 20, 'programs': 30}
The datatype of the given dictionary after conversion = <class 'str'>

Method #2: Using for loop and join() Function

To convert a dictionary to a string, we use the join() method of string and a for loop here. The join method concatenates all of the elements of an iterable into a single string.

Syntax: 

str_seperator.join(iterable)

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Print the given original dictionary
  • Print the datatype of the given original dictionary using the type() function
  • Add all the key and values of the dictionary using list comprehension and convert them to a string using the join() function.
  • Store it in another variable.
  • Print the given dictionary after conversion to string.
  • Print the datatype of the given dictionary after conversion using the type() function.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {"hello": 10 ,"Python": 20 ,"programs": 30}
# Print the given original dictionary
print("The given original dictionary = ", gvn_dict)
# Print the datatype of the given original dictionary using the type() function
print("The datatype of the given original dictionary = ", type(gvn_dict))
print()

# Add all the key and values of the dictionary using list comprehension and
# convert them to string using the join() function.
str_conversion = ', '.join(str(key)+':'+str(value) for key, value in gvn_dict.items())

# Print the given dictionary after conversion to string.
print("The given dictionary after conversion to string = ", str_conversion)
# Print the datatype of the given original dictionary using the type() function
print("The datatype of the given dictionary after conversion = ", type(str_conversion))

Output:

The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}
The datatype of the given original dictionary = <class 'dict'>

The given dictionary after conversion to string = hello:10, Python:20, programs:30
The datatype of the given dictionary after conversion = <class 'str'>

Method #3: Using json.dumps() Function

JSON is a JavaScript Object Notation that typically stores data as key-value pairs.

The json.dumps() method converts Python dictionaries object into serialized JSON string objects.

By passing the given dictionary to the json.dumps() method, it returns the result as a string.

Syntax:

json.dumps(dict)

Approach:

  • Import json module using the import keyword
  • Give the dictionary as static input and store it in a variable.
  • Print the given original dictionary
  • Print the datatype of the given original dictionary using the type() function
  • Pass the given dictionary to the dumps() method of the json module to convert the given dictionary to a string.
  • Print the given dictionary after conversion to string.
  • Print the datatype of the given original dictionary using the type() function.
  • The Exit of the Program.

Below is the implementation:

# Import json module using the import keyword
import json
# Give the dictionary as static input and store it in a variable.
gvn_dict = {"hello": 10 ,"Python": 20 ,"programs": 30}
# Print the given original dictionary
print("The given original dictionary = ", gvn_dict)
# Print the datatype of the given original dictionary using the type() function
print("The datatype of the given original dictionary = ", type(gvn_dict))
print()

# Pass the given dictionary to the dumps() method of the json module
# to convert the given dictionary to a string
str_conversion = json.dumps(gvn_dict)

# Print the given dictionary after conversion to string.
print("The given dictionary after conversion to string = ", str_conversion)
# Print the datatype of the given original dictionary using the type() function
print("The datatype of the given dictionary after conversion = ", type(str_conversion))

Output:

The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}
The datatype of the given original dictionary = <class 'dict'>

The given dictionary after conversion to string = {"hello": 10, "Python": 20, "programs": 30}
The datatype of the given dictionary after conversion = <class 'str'>