. Advertisement .
..3..
. Advertisement .
..4..
Python has many data structures to deal with, each of which brings something new to the table. We will learn what is the best way to convert dict to string in Python.
Because we frequently need to convert from one data structure to another to convey data smoothly.
Python: Convert dict to string using several methods
Converting a dictionary to a string in Python may be done in several ways. Let’s take a look at a few of the more popular approaches.
The str() Method
We will send to the str() function a dictionary object in this approach of converting a dictionary to a string.
# Import Python json module
import json
# Create a Python dictionary
dict1 = {'Strawberry': 7, 'Mangosteen': 6, 'Pear': 15, 'Pineapple': 24, 'Durian': 29}
print(type(dict1))
print(f"Given dictionary: {dict1}")
# Convert the dictionary to a string using json.dumps() function
string = json.dumps(dict1)
print(type(string))
print(f"Converted string: {string}")
Output
<class 'dict'>
Given dictionary: {'Strawberry': 7, 'Mangosteen': 6, 'Pear': 15, 'Pineapple': 24, 'Durian': 29}
<class 'str'>
Converted string: {"Strawberry": 7, "Mangosteen": 6, "Pear": 15, "Pineapple": 24, "Durian": 29}
Utilising An Empty String As The Basis For A Loop
We will access the dictionary’s keys by iterating over the dictionary object using a loop in the following approach for converting a
dictionary to a string. Then, we get the value for each key, and then put them together: an empty string as a value pair.
# Create a Python dictionary
dict2 = {'T': "Toyota", 'F': "Ford", 'M': "Mitsubishi", 'H': "Honda"}
print(type(dict2))
print(f"Given dictionary- {dict2}")
# Create an empty string
string = ""
# Convert the dictionary to a string using for loop only
for i in dict:
string += i + ':' + dict2[i] + ' '
print(type(string))
print(f"Converted string- {string}")
Output
<class 'dict'>
Given dictionary- {'T': 'Toyota', 'F': 'Ford', 'M': 'Mitsubishi', 'H': 'Honda'}
<class 'str'>
Converted string- T:Toyota F:Ford M:Mitsubishi H:Honda
Using The items() Method, An Empty String, And A For Loop
We need to gain access to the key:value pair first by iterating over a for loop on the dictionary object and the items() function to convert a dictionary to a string. Each key: value pair is then appended to an empty string.
# Create a Python dictionary
dict3 = {'Google': "GOOGL", 'Microsoft': "MSFT", 'Aplle': "AAPL"}
print(type(dict3))
print(f"Given dictionary- {dict3}")
# Create an empty string
string = ""
# Convert the dictionary to a string using for loop and items() function
for key, value in dict3.items():
string += key + ':' + value + ' '
print(type(string))
print(f"Converted string- {string}")
Output
<class 'dict'>
Given dictionary- {'Google': 'GOOGL', 'Microsoft': 'MSFT', 'Aplle': 'AAPL'}
<class 'str'>
Converted string- Google:GOOGL Microsoft:MSFT Aplle:AAPL
Using A For Loop, items(), and str.join() Method
We use a for loop to traverse over the dictionary object and add the key and its value in this way of converting a dictionary to a string. The str.join() method will then be used to unite all of the key: value pairs into a single string.
# Create a Python dictionary
dict4 = {'Google': " - Android", 'Microsoft': " - Windows Phone", 'Apple': " - IOS"}
print(type(dict4))
print(f"Given dictionary: {dict4}")
# Convert the dictionary to a string using str.join() and items() function
string = ', '.join(key + value for key, value in dict4.items())
print(type(string))
print(f"Converted string: {string}")
Output
<class 'dict'>
Given dictionary: {'Google': ' - Android', 'Microsoft': ' - Windows Phone', 'Apple': ' - IOS'}
<class 'str'>
Converted string: Google - Android, Microsoft - Windows Phone, Apple - IOS
Conclusion
This article taught us how to convert dict to string python using several methods. We hope you have grasped the concepts described above and will put them into practice on your own.
2 Comments