Here are 2 ways to convert a dictionary to a string in Python:
(1) Using json.dumps() to convert dictionary to string:
import json my_dictionary = {1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"} my_string = json.dumps(my_dictionary) print(my_string) print(type(my_string))
The result is a string:
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}
<class 'str'>
Examples of different variations of dictionaries (inside a list) converted into strings using json.dumps():
import json
list_of_dictionaries = [{1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"},
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"},
{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'},
{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'},
{"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5},
{"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"},
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5},
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}
]
for i in list_of_dictionaries:
my_string = json.dumps(i)
print(my_string)
Here are the results:
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}
{"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}
{"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5}
{"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"}
{"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5}
{"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"}
(2) Using str() to convert dictionary to string:
my_dictionary = {1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"} my_string = str(my_dictionary) print(my_string) print(type(my_string))
The result is a string:
{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}
<class 'str'>
Examples of different variations of dictionaries (inside a list) converted into strings using str():
list_of_dictionaries = [{1: "blue", 2: "green", 3: "red", 4: "yellow", 5: "purple"}, {"1": "blue", "2": "green", "3": "red", "4": "yellow", "5": "purple"}, {1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}, {'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}, {"blue": 1, "green": 2, "red": 3, "yellow": 4, "purple": 5}, {"blue": "1", "green": "2", "red": "3", "yellow": "4", "purple": "5"}, {'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}, {'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'} ] for i in list_of_dictionaries: my_string = str(i) print(my_string)
The output:
{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}
{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}
{1: 'blue', 2: 'green', 3: 'red', 4: 'yellow', 5: 'purple'}
{'1': 'blue', '2': 'green', '3': 'red', '4': 'yellow', '5': 'purple'}
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}
{'blue': 1, 'green': 2, 'red': 3, 'yellow': 4, 'purple': 5}
{'blue': '1', 'green': '2', 'red': '3', 'yellow': '4', 'purple': '5'}
Additional articles: