How to convert a dictionary into a string in Python

In this Python tutorial, we will learn how to convert a Python dictionary into a string.  To understand various approaches, we’ll use some built-in functions to convert a Python dictionary into a string. 

As a Developer, while making the Python Project I got the requirement to convert a Python dictionary into a string. 

Here we will see:

  • How to convert a dictionary into a string in Python using str()
  • How to convert a dictionary into a string in Python using json.dumps()
  • How to convert a dictionary into a string in Python using items(), and str.join()
  • How to convert a dictionary into a string in Python using a conventional way

Convert a dictionary into a string in Python

In Python, there are numerous methods for converting a Dictionary into a string. We’ll go through how to Convert a dictionary into a string in Python specifically utilizing the str() method, and json.dumps() method.

How to convert a dictionary into a string in Python using str()

  • In this section, we will discuss how to convert a dictionary into a string in Python using str().
  • The dictionary object will simply be passed to the str() function in this way of dictionary-to-string conversion.
  • Python’s str function returns the string variant of the provided object or integer. The method will return an empty string if no parameters are supplied.

Example:

Let’s take an example and check how to convert a dictionary into a string in Python using str().

Source Code:

# Create a Python dictionary
Country_name = {'U.S.A': 8965, 'Germany': 4562, 'China': 9234, 'Australia': 8562}
print("Input dictionary :",Country_name )
print(type(Country_name))
# By using the str() function
new_result = str(Country_name)
print("Converted dictionary into string :",new_result)
print(type(new_result))

In the following given code first, we will create a dictionary and then use the type() function to check the object. Next, we used the str() function and within this function, we passed the input dictionary as an argument.

Here is the Screenshot of the following given code.

How to convert a dictionary into a string in Python using str
Converting a dictionary into a string in Python using str

This is how to convert a dictionary into a string in Python using str.

Read: Convert string to float in Python

How to convert a dictionary into a string in Python using json.dumps()

  • Here we will discuss how to convert a dictionary into a string in Python using json.dump().
  • The json.dump() method enables us to transform a Python object into a JSON object equivalent and save the outcome as a file.
  • A Python dictionary is transformed into a string using the json.dumps() method, whereas the json.loads() technique works in the exact opposite way.

Example:

Let’s take an example and check how to convert a dictionary into a string in Python using json.dump().

Source Code:

import json
Cars_in_USA = {'BMW': 7345,'Harley Davidson':4352}
new_val = json.dumps(Cars_in_USA )
print("Converted dictionary into string :",new_val )
print(type(new_val))

In the above code first, we imported the JSON module and then created the input dictionary named “Cars_in_USA”. Next, we used the json.dumps() function and it will convert the dictionary into a string.

You can refer to the below Screenshot.

How to convert a dictionary into a string in Python using json.dumps_
Converting a dictionary into a string in Python using json.dumps_

As you can see in the Screenshot we have discussed how to convert a dictionary into a string in Python using json.dumps().

Read: Python convert binary to decimal

How to convert a dictionary into a string in Python using items(), and str.join()

  • Now let us understand how to convert a dictionary into a string in Python using items(), and str.join().
  • Using a for loop, we will cycle through the dictionary object and add the key and its value in this way of converting a dictionary to a string. The key: value pairs will then be combined into a single string using the str.join() function.

Example:

Here we will take an example and check how to convert a dictionary into a string in Python using items(), and str.join().

Source Code:

# Input Dictionary
Cities_in_USA = {'NewYork': '7862', 'LosAngeles': '4253', 'Chicago': '2987'}
print("Input dictionary :", Cities_in_USA)

# By using the str.join() and items() function
new_result = ', '.join(key + value for key, value in Cities_in_USA.items())
print(new_result)
print(type(new_result))

Here is the execution of the following given code.

How to convert a dictionary into a string in Python using items and str.join_
Converting a dictionary into a string in Python using items and str.join()

In this example, we have understood how to convert a dictionary into a string in Python using items and str.join().

Read: Convert tuple to string in Python

How to convert a dictionary into a string in Python using a conventional way

  • In this section, we will discuss how to convert a dictionary into a string in Python using a conventional way.
  • We can always convert a dict to a string in Python by using the conventional process in addition to utilizing the built-in functions. The conventional method is to concatenate the newly created strings by iterating through the dictionary using a for loop.
  • In this example, we will use two main operations first, we will iterate over the dictionary using the for loop and then concatenate strings.

Example:

Let’s take an example and check how to convert a dictionary into a string in Python using a conventional way.

Source Code:

cars_in_USA = {"Volkswagen": "8927", "Audi": "5632", "BMW": "1782"}
print("Input Dictionary: ", cars_in_USA)
# Empty string
new_convt = str()

# iterating over dictionary using a for loop
for new_key in cars_in_USA:
    new_convt += new_key + ": " + cars_in_USA[new_key] + ", "

print("Output string: " + new_convt + " type: ", type(new_convt))

Here is the execution of the following given code.

How to convert a dictionary into a string in Python using a conventional way
Converting a dictionary into a string in Python using a conventional way

You may also like to read the following Python tutorials.

In this article, we have learned how to convert a Python dictionary into a string. And also we have covered the following given topics.

  • How to convert a dictionary into a string in Python using str()
  • How to convert a dictionary into a string in Python using json.dumps()
  • How to convert a dictionary into a string in Python using items(), and str.join()
  • How to convert a dictionary into a string in Python using a conventional way