How to convert a dictionary to a string in Python

In this tutorial, we will learn various ways to convert a dictionary to a string in Python.

The most common ways are:

  • Using str() method
  • With help of for loop and join() method
  • Using json.dumps() method

Let us discuss each of them clearly in a simple way.

Using the str() method to convert a dictionary to a string in Python

We can easily convert a dictionary to a string with the help of the str() method. The resulting string includes the whole dictionary keys, values, colon and even the curly braces.

Syntax:
str(dictionary)

Program to convert a dictionary to a string using the str() method

d={"name":"Ravi","age":25,"salary":1000.50}
print("Before converting:",d)
print(type(d))

s=str(d)
print("After converting:",s)
print(type(s))

Output:

Before converting: {'name': 'Ravi', 'age': 25, 'salary': 1000.5}
<class 'dict'>
After converting: {'name': 'Ravi', 'age': 25, 'salary': 1000.5}
<class 'str'>

In the above example, we can see that the type changed from ‘dict’ to ‘str’.

Using for loop and join() method

In this method, we will use the join() method of string and a for loop to convert a dictionary to a string. The join method combines all the elements of iterable to a single string.

Syntax of join() method:
string_seperator.join(iterable)

Since the for loop can iterate through the dictionary, we will use this loop in the place of iterable in the join method. The for loop iterates over the key values of the dictionary using dict.items() method.

As a result, the final string consists of key values separated by a comma which is used in place of the string separator of the join method.

Program to convert a dictionary to a string using for loop and join() method

d={"name":"Ravi","age":25,"salary":1000.50}
print("Before converting:",d)
print(type(d))

s = ', '.join(str(key)+':'+str(value) for key, value in d.items())
    
print("After converting:",s)
print(type(s))

Output:

Before converting: {'name': 'Ravi', 'age': 25, 'salary': 1000.5}
<class 'dict'>
After converting: name:Ravi, age:25, salary:1000.5
<class 'str'>

Using json.dumps() method

JSON is a JavaScript Object Notation that usually stores information in the form of key-value pairs. The json.dumps() method converts dictionaries of python objects into serialized json string objects.

We will pass our dictionary to the json.dumps() method and it returns the result in the form of a string.

Syntax:
json.dumps(dictionary)

Program to convert a dictionary to a string using json.dumps() method

import json
d={"name":"Ravi","age":25,"salary":1000.50}
print("Before converting:",d)
print(type(d))

s=json.dumps(d)
    
print("After converting:",s)
print(type(s))

Output:

Before converting: {'name': 'Ravi', 'age': 25, 'salary': 1000.5}
<class 'dict'>
After converting: {"name": "Ravi", "age": 25, "salary": 1000.5}
<class 'str'>

That’s it! Hope you understood how to convert a dictionary to a string. If you have any doubts, feel free to post them below.

Also, do check our other related articles,

Leave a Reply

Your email address will not be published.