{"id":27163,"date":"2022-04-06T22:03:52","date_gmt":"2022-04-06T16:33:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=27163"},"modified":"2022-04-06T22:03:52","modified_gmt":"2022-04-06T16:33:52","slug":"how-to-convert-dictionary-to-a-string-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-convert-dictionary-to-a-string-in-python\/","title":{"rendered":"How to Convert Dictionary to a String in Python?"},"content":{"rendered":"
Dictionary in Python:<\/strong><\/p>\n A dictionary is a Python object that stores data in the key:value format. A colon(:) separates the key and its corresponding value. A comma (,) separates each key:value pair in the dictionary.\u00a0 In Python, a dictionary is always enclosed in curly brackets.<\/p>\n A Python dictionary, unlike a list or a tuple, is an unordered data structure. We can directly access any value in a Python dictionary by using the key that corresponds to it.<\/p>\n In Python, there are various methods for converting a dictionary to a string.<\/p>\n The most common methods are as follows:<\/p>\n The str() method is a very simple method to convert a dictionary to a string. The resulting string contains all of the dictionary’s keys, values, colon, and even curly braces.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To convert a dictionary to a string, we use the join() method of string and a for loop here. The join method concatenates all of the elements of an iterable into a single string.<\/p>\n Syntax:\u00a0<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n JSON is a JavaScript Object Notation that typically stores data as key-value pairs.<\/p>\n The json.dumps() method converts Python dictionaries object into serialized JSON string objects.<\/p>\n By passing the given dictionary to the json.dumps() method, it returns the result as a string.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Dictionary in Python: A dictionary is a Python object that stores data in the key:value format. A colon(:) separates the key and its corresponding value. A comma (,) separates each key:value pair in the dictionary.\u00a0 In Python, a dictionary is always enclosed in curly brackets. A Python dictionary, unlike a list or a tuple, is …<\/p>\nConverting Dictionary to a String in Python<\/h2>\n
\n
Method #1: Using str() Function<\/h3>\n
str(dict)<\/pre>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {\"hello\": 10 ,\"Python\": 20 ,\"programs\": 30}\r\n# Print the given original dictionary\r\nprint(\"The given original dictionary = \", gvn_dict)\r\n# Print the datatype of the given original dictionary using the type() function\r\nprint(\"The datatype of the given original dictionary = \", type(gvn_dict))\r\nprint()\r\n\r\n# Convert the given dictionary to string using the str() function by passing \r\n# given dictionary as an argument to it.\r\n# Store it in another variable.\r\nstr_conversion =str(gvn_dict)\r\n# Print the given dictionary after conversion to string.\r\nprint(\"The given dictionary after conversion to string = \", str_conversion)\r\n# Print the datatype of the given dictionary after conversion using the type() function\r\nprint(\"The datatype of the given dictionary after conversion = \", type(str_conversion))\r\n<\/pre>\n
The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}\r\nThe datatype of the given original dictionary = <class 'dict'>\r\n\r\nThe given dictionary after conversion to string = {'hello': 10, 'Python': 20, 'programs': 30}\r\nThe datatype of the given dictionary after conversion = <class 'str'><\/pre>\n
Method #2: Using for loop and join() Function<\/h3>\n
str_seperator.join(iterable)<\/pre>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {\"hello\": 10 ,\"Python\": 20 ,\"programs\": 30}\r\n# Print the given original dictionary\r\nprint(\"The given original dictionary = \", gvn_dict)\r\n# Print the datatype of the given original dictionary using the type() function\r\nprint(\"The datatype of the given original dictionary = \", type(gvn_dict))\r\nprint()\r\n\r\n# Add all the key and values of the dictionary using list comprehension and\r\n# convert them to string using the join() function.\r\nstr_conversion = ', '.join(str(key)+':'+str(value) for key, value in gvn_dict.items())\r\n\r\n# Print the given dictionary after conversion to string.\r\nprint(\"The given dictionary after conversion to string = \", str_conversion)\r\n# Print the datatype of the given original dictionary using the type() function\r\nprint(\"The datatype of the given dictionary after conversion = \", type(str_conversion))\r\n<\/pre>\n
The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}\r\nThe datatype of the given original dictionary = <class 'dict'>\r\n\r\nThe given dictionary after conversion to string = hello:10, Python:20, programs:30\r\nThe datatype of the given dictionary after conversion = <class 'str'><\/pre>\n
Method #3: Using json.dumps() Function<\/h3>\n
json.dumps(dict)<\/pre>\n
\n
# Import json module using the import keyword\r\nimport json\r\n# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {\"hello\": 10 ,\"Python\": 20 ,\"programs\": 30}\r\n# Print the given original dictionary\r\nprint(\"The given original dictionary = \", gvn_dict)\r\n# Print the datatype of the given original dictionary using the type() function\r\nprint(\"The datatype of the given original dictionary = \", type(gvn_dict))\r\nprint()\r\n\r\n# Pass the given dictionary to the dumps() method of the json module\r\n# to convert the given dictionary to a string\r\nstr_conversion = json.dumps(gvn_dict)\r\n\r\n# Print the given dictionary after conversion to string.\r\nprint(\"The given dictionary after conversion to string = \", str_conversion)\r\n# Print the datatype of the given original dictionary using the type() function\r\nprint(\"The datatype of the given dictionary after conversion = \", type(str_conversion))\r\n<\/pre>\n
The given original dictionary = {'hello': 10, 'Python': 20, 'programs': 30}\r\nThe datatype of the given original dictionary = <class 'dict'>\r\n\r\nThe given dictionary after conversion to string = {\"hello\": 10, \"Python\": 20, \"programs\": 30}\r\nThe datatype of the given dictionary after conversion = <class 'str'><\/pre>\n