How to Convert a Dictionary to a String in Python? [3 useful methods]

In Python, a dictionary is an unordered collection of key-value pairs, where each key is associated with a value. Sometimes, we may need to convert a dictionary to a string for various purposes, such as formatting the output, saving the data to a file, or sending it over a network. In this tutorial, we’ll learn different ways to convert a dictionary to a string in Python using various examples.

Here we will take the below examples:

city_dict = {
"New York": "New York",
"Los Angeles": "California",
"Chicago": "Illinois",
"Houston": "Texas",
"Phoenix": "Arizona"
}

Convert a Dictionary to a String in Python

We will see below 3 ways to convert a dictionary to a string in Python.

  • Using the str() function
  • Using the json.dumps() function
  • Using a custom function

Method 1: Using the str() function

The simplest way to convert a dictionary to a string in Python is by using the built-in str() function. This function takes the dictionary as an argument and returns a string representation of the dictionary.

city_dict = {
    "New York": "New York",
    "Los Angeles": "California",
    "Chicago": "Illinois",
    "Houston": "Texas",
    "Phoenix": "Arizona"
}

city_str = str(city_dict)
print(city_str)

Output:

{
    'New York': 'New York',
    'Los Angeles': 'California',
    'Chicago': 'Illinois',
    'Houston': 'Texas',
    'Phoenix': 'Arizona'
}

You can see the output here:

convert dictionary to a string in Python
convert dictionary to a string in Python

Method 2: Using the json.dumps() function

Another way to convert a dictionary to a string is by using the json.dumps() function from the JSON library in Python. This method is useful when you want to convert the dictionary to a JSON-formatted string.

import json

city_dict = {
    "New York": "New York",
    "Los Angeles": "California",
    "Chicago": "Illinois",
    "Houston": "Texas",
    "Phoenix": "Arizona"
}

city_json_str = json.dumps(city_dict, indent=4)
print(city_json_str)

Output:

{
    "New York": "New York",
    "Los Angeles": "California",
    "Chicago": "Illinois",
    "Houston": "Texas",
    "Phoenix": "Arizona"
}

Method 3: Using a custom function

You can also create a custom function to convert a dictionary to a string in Python in your desired format. This method gives you more control over the output format.

def dict_to_string(city_dict):
    result = ""
    for key, value in city_dict.items():
        result += f"{key}, {value}\n"
    return result

city_dict = {
    "New York": "New York",
    "Los Angeles": "California",
    "Chicago": "Illinois",
    "Houston": "Texas",
    "Phoenix": "Arizona"
}

custom_str = dict_to_string(city_dict)
print(custom_str)

Output:

New York, New York
Los Angeles, California
Chicago, Illinois
Houston, Texas
Phoenix, Arizona

In this tutorial, we’ve covered three different methods to convert a dictionary to a string in Python. By understanding the str() function, json.dumps(), and custom functions, you can choose the most appropriate approach for your specific requirements.

You may also like the following Python tutorials: