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:
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:
- Create a String with Double Quotes in Python
- Create String with Single Quotes in Python
- Python Get First Key in Dictionary
- Python dictionary keys() method [With Examples]
- Python dictionary pop() method
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.