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.
Methods to convert a dictionary into a string in Python
Here we will see:
- How dict to string 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 dict to string 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:
Country_name = {'U.S.A': 8965, 'Germany': 4562, 'China': 9234, 'Australia': 8562}
print("Input dictionary :",Country_name )
print(type(Country_name))
new_result = str(Country_name)
print("Converted dictionary into string :",new_result)
print(type(new_result))
Output: 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.
Input dictionary : {'U.S.A': 8965, 'Germany': 4562, 'China': 9234, 'Australia': 8562}
<class 'dict'>
Converted dictionary into string : {'U.S.A': 8965, 'Germany': 4562, 'China': 9234, 'Australia': 8562}
<class 'str'>
Here is the Screenshot of the following given code:
This is how to convert a dictionary into a string in Python using str.
how to convert dict to 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))
Output: 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.
Converted dictionary into string : {"BMW": 7345, "Harley Davidson": 4352}
<class 'str'>
You can refer to the below Screenshot.
As you can see in the Screenshot we have discussed how to convert a dictionary into a string in Python using json.dumps().
How Python convert dict to string 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:
Cities_in_USA = {'NewYork': '7862', 'LosAngeles': '4253', 'Chicago': '2987'}
print("Input dictionary :", Cities_in_USA)
new_result = ', '.join(key + value for key, value in Cities_in_USA.items())
print(new_result)
print(type(new_result))
Output: Here is the execution of the following given code.
Input dictionary : {'NewYork': '7862', 'LosAngeles': '4253', 'Chicago': '2987'}
NewYork7862, LosAngeles4253, Chicago2987
<class 'str'>
In this example, we have understood how to convert a dictionary into a string in Python using items and str.join().
How Python dictionary to string 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)
new_convt = str()
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))
Output: Here is the execution of the following given code.
Input Dictionary: {'Volkswagen': '8927', 'Audi': '5632', 'BMW': '1782'}
Output string: Volkswagen: 8927, Audi: 5632, BMW: 1782, type: <class 'str'>
You may also like to read the following Python tutorials.
- Python convert dictionary to an array
- How to convert a String to DateTime in Python
- How to convert dictionary to JSON in Python
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
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.