{"id":19587,"date":"2021-07-01T10:42:00","date_gmt":"2021-07-01T10:42:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19587"},"modified":"2023-06-28T10:26:06","modified_gmt":"2023-06-28T10:26:06","slug":"dictionary-to-a-string","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/dictionary-to-a-string","title":{"rendered":"How to convert a dictionary to a string in Python?"},"content":{"rendered":"\n<p>Dive into the exciting world of Python by exploring various techniques to transform dictionaries into strings. This comprehensive guide unveils the different methods and scenarios where each can be most effective, enabling you to harness Python&#8217;s powerful features.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Python offers various methods to convert a dictionary into a string. These include using the str() function, the json.dumps() function, and several loop constructs. The conversion can be tailored to include both keys and values, or either of them separately.<\/em><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a dictionary in Python?<\/h2>\n\n\n\n<p>A <strong>dictionary <\/strong>is a Python object which is used to store the data in the <strong>key:value<\/strong> format. The key and its corresponding value are separated by a colon (:). And each key:value pair in the dictionary is separated by a comma (,). A dictionary in Python is always enclosed in the curly brackets {}. <\/p>\n\n\n\n<p>A Python dictionary is an <strong>unordered <\/strong>data structure unlike a list or a tuple in Python. We can directly access any value of a Python dictionary just by using its corresponding key. Let&#8217;s see how we can create a dictionary through Python code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndc = {&#039;py&#039;: &quot;PYTHON&quot;, &#039;cpp&#039;: &quot;C++&quot;, &#039;mat&#039;: &quot;MATLAB&quot;}\nprint(type(dc))\nprint(dc)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \n{&#039;py&#039;: &#039;PYTHON&#039;, &#039;cpp&#039;: &#039;C++&#039;, &#039;mat&#039;: &#039;MATLAB&#039;}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What is a string in Python?<\/h2>\n\n\n\n<p>A <strong>string <\/strong>is also a Python object which is the most commonly used data structure and it is used to store a sequence of characters. Anything enclosed inside the single, double, or triple quotes is a string in Python. In Python, the single and the double quotes can be used interchangeably to represent a single line string while triple quotes are used to store multi-line strings. Let&#8217;s create one string through Python code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\nsr = &quot;py: PYTHON cpp: C++ mat: MATLAB&quot;\nprint(type(sr))\nprint(sr)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;str&#039;&gt; \npy: PYTHON cpp: C++ mat: MATLAB\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Different ways to convert a dictionary to a string in Python<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">In Python, there are multiple ways to convert a dictionary to a string. Let&#8217;s discuss some of the most commonly used methods\/ways to do it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using the str() function<\/h3>\n\n\n\n<p>In this method of converting a dictionary to a string, we will simply pass the dictionary object to the <code>str()<\/code> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndc = {&#039;A&#039;: &#039;Android&#039;, &#039;B&#039;: &#039;Bootstrap&#039;, &#039;C&#039;: &#039;C Programming&#039;, &#039;D&#039;: &#039;Dart&#039;}\nprint(type(dc))\nprint(f&quot;Given dictionary: {dc}&quot;)\n\n# Convert the dictionary to a string\n# using str() function\nsr = str(dc)\nprint(type(sr))\nprint(f&quot;Converted string: {sr}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary: {&#039;A&#039;: &#039;Android&#039;, &#039;B&#039;: &#039;Bootstrap&#039;, &#039;C&#039;: &#039;C Programming&#039;, &#039;D&#039;: &#039;Dart&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nConverted string: {&#039;A&#039;: &#039;Android&#039;, &#039;B&#039;: &#039;Bootstrap&#039;, &#039;C&#039;: &#039;C Programming&#039;, &#039;D&#039;: &#039;Dart&#039;}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Using json.dumps() function<\/h3>\n\n\n\n<p>In this method of converting a dictionary to a string, we will pass the dictionary object to the <code>json.dumps()<\/code> function. To use the <code>json.dumps()<\/code> function we have to import the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-json-module\" data-type=\"post\" data-id=\"3330\">JSON module<\/a> which is a built-in package in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python json module\nimport json\n\n# Create a Python dictionary\ndict = {&#039;A&#039;: 1, &#039;B&#039;: 2, &#039;C&#039;: 3, &#039;D&#039;: 4, &#039;E&#039;: 5}\nprint(type(dict))\nprint(f&quot;Given dictionary: {dict}&quot;)\n\n# Convert the dictionary to a string\n# using json.dumps() function\nstr = json.dumps(dict)\nprint(type(str))\nprint(f&quot;Converted string: {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary: {&#039;A&#039;: 1, &#039;B&#039;: 2, &#039;C&#039;: 3, &#039;D&#039;: 4, &#039;E&#039;: 5} \n&amp;lt;class &#039;str&#039;&gt; \nConverted string: {&quot;A&quot;: 1, &quot;B&quot;: 2, &quot;C&quot;: 3, &quot;D&quot;: 4, &quot;E&quot;: 5}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Using an empty string and for loop<\/h3>\n\n\n\n<p>In this method of converting a dictionary to a string, we will simply access the keys of the dictionary by iterating through the dictionary object using a <a href=\"https:\/\/www.askpython.com\/course\/python-course-for-loop\" data-type=\"post\" data-id=\"18443\"><code>for<\/code> loop<\/a>. Then we access the value corresponding to each key and add the key: value pair to an empty string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;D&#039;: &quot;Debian&quot;, &#039;U&#039;: &quot;Ubuntu&quot;, &#039;C&#039;: &quot;CentOS&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary- {dict}&quot;)\n\n# Create an empty string\nstr = &quot;&quot;\n\n# Convert the dictionary to a string\n# using for loop only\nfor item in dict:\n    str += item + &#039;:&#039; + dict&#x5B;item] + &#039; &#039;\nprint(type(str))\nprint(f&quot;Converted string- {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary- {&#039;D&#039;: &#039;Debian&#039;, &#039;U&#039;: &#039;Ubuntu&#039;, &#039;C&#039;: &#039;CentOS&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nConverted string- D:Debian U:Ubuntu C:CentOS\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. Using an empty string, a for loop, and items() function<\/h3>\n\n\n\n<p>In this method of converting a dictionary to a string, we will first access the key:value pair by iterating through the dictionary object using a <code>for<\/code> loop and <code>items()<\/code> function. Then we add each key:value pair to an empty string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;Google&#039;: &quot;GCP&quot;, &#039;Microsoft&#039;: &quot;Azure&quot;, &#039;Amazon&#039;: &quot;AWS&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary- {dict}&quot;)\n\n# Create an empty string\nstr = &quot;&quot;\n\n# Convert the dictionary to a string\n# using for loop and items() function\nfor key, value in dict.items():\n    str += key + &#039;:&#039; + value + &#039; &#039;\nprint(type(str))\nprint(f&quot;Converted string- {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary- {&#039;Google&#039;: &#039;GCP&#039;, &#039;Microsoft&#039;: &#039;Azure&#039;, &#039;Amazon&#039;: &#039;AWS&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nConverted string- Google:GCP Microsoft:Azure Amazon:AWS  \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. Using a for loop, items(), and str.join() function<\/h3>\n\n\n\n<p>In this method of converting a dictionary to a string, we will iterate through the dictionary object using a <code>for<\/code> loop and add the key &#038; its value. Then we will use the <code>str.join()<\/code> function to join all the key: value pairs together as one single string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;Google&#039;: &quot; Chrome&quot;, &#039;Microsoft&#039;: &quot; Edge&quot;, &#039;Apple&#039;: &quot; Safari&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary: {dict}&quot;)\n\n# Convert the dictionary to a string\n# using str.join() and items() function\nstr = &#039;, &#039;.join(key + value for key, value in dict.items())\nprint(type(str))\nprint(f&quot;Converted string: {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary: {&#039;Google&#039;: &#039;-Chrome&#039;, &#039;Microsoft&#039;: &#039;-Edge&#039;, &#039;Apple&#039;: &#039;-Safari&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nConverted string: Google-Chrome, Microsoft-Edge, Apple-Safari\n<\/pre><\/div>\n\n\n<p>In Python, we can also convert the keys and values of a dictionary object into two separate strings rather than converting the key: value pairs into a single string. Let&#8217;s discuss it one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Convert the keys of a dictionary to a string<\/h2>\n\n\n\n<p>First, we will discuss the different ways to convert the keys of the dictionary object to a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using an empty string and for loop<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;OS&#039;: &quot;Operating System&quot;, &#039;DBMS&#039;: &quot;Database Management System&quot;, &#039;CN&#039;: &quot;Computer Network&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary- {dict}&quot;)\n\n# Create an empty string\nstr = &quot;&quot;\n\n# Convert the dictionary keys into a string\n# using for loop only\nfor item in dict:\n    str += item + &quot; &quot;\nprint(type(str))\nprint(f&quot;Keys in string- {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary- {&#039;OS&#039;: &#039;Operating System&#039;, &#039;DBMS&#039;: &#039;Database Management System&#039;, &#039;CN&#039;: &#039;Computer Network&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nKeys in string- OS DBMS CN\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Using a for loop and str.join() function<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;gcc&#039;: &quot;C program&quot;, &#039;g++&#039;: &quot;C++ program&quot;, &#039;py&#039;: &quot;Python Program&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary: {dict}&quot;)\n\n# Convert the dictionary keys into a string\n# using str.join()\nstr = &#039;, &#039;.join(key for key in dict)\nprint(type(str))\nprint(f&quot;Keys in string: {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary: {&#039;gcc&#039;: &#039;C program&#039;, &#039;g++&#039;: &#039;C++ program&#039;, &#039;py&#039;: &#039;Python Program&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nKeys in string: gcc, g++, py\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Convert the values of a dictionary to a string<\/h2>\n\n\n\n<p>Now let&#8217;s discuss the different ways to convert the values of the dictionary object to a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using an empty string and for loop<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;OS&#039;: &quot;Operating_System&quot;, &#039;DBMS&#039;: &quot;Database_Management_System&quot;, &#039;CN&#039;: &quot;Computer_Network&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary- {dict}&quot;)\n\n# Create an empty string\nstr = &quot;&quot;\n\n# Convert the dictionary values into a string\n# using for loop only\nfor item in dict:\n    str += dict&#x5B;item] + &quot; &quot;\nprint(type(str))\nprint(f&quot;Values in string- {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary- {&#039;OS&#039;: &#039;Operating_System&#039;, &#039;DBMS&#039;: &#039;Database_Management_System&#039;, &#039;CN&#039;: &#039;Computer_Network&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nValues in string- Operating_System Database_Management_System Computer_Network \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Using a for loop and str.join() function<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create a Python dictionary\ndict = {&#039;gcc&#039;: &quot;C program&quot;, &#039;g++&#039;: &quot;C++ program&quot;, &#039;py&#039;: &quot;Python Program&quot;}\nprint(type(dict))\nprint(f&quot;Given dictionary: {dict}&quot;)\n\n# Convert the dictionary values into a string\n# using str.join()\nstr = &#039;, &#039;.join(dict&#x5B;item] for item in dict)\nprint(type(str))\nprint(f&quot;Values in string: {str}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;dict&#039;&gt; \nGiven dictionary: {&#039;gcc&#039;: &#039;C program&#039;, &#039;g++&#039;: &#039;C++ program&#039;, &#039;py&#039;: &#039;Python Program&#039;} \n&amp;lt;class &#039;str&#039;&gt; \nValues in string: C program, C++ program, Python Program\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>After exploring the myriad ways to convert Python dictionaries into strings, you should now be proficient in applying these techniques, even for key-value separation. Ready to apply these concepts in your next Python project?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dive into the exciting world of Python by exploring various techniques to transform dictionaries into strings. This comprehensive guide unveils the different methods and scenarios where each can be most effective, enabling you to harness Python&#8217;s powerful features. Python offers various methods to convert a dictionary into a string. These include using the str() function, [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":19673,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-19587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19587"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19587\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/19673"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}