{"id":2562,"date":"2020-08-06T06:49:33","date_gmt":"2020-08-06T06:49:33","guid":{"rendered":"https:\/\/www.afternerd.com\/blog\/?p=2562"},"modified":"2020-08-06T06:58:09","modified_gmt":"2020-08-06T06:58:09","slug":"python-convert-list-string","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/python-convert-list-string\/","title":{"rendered":"Python: How to Convert a List into a String? (The Right Way)"},"content":{"rendered":"\n<p>Converting a list of strings into a string is a very common operation.<\/p>\n\n\n\n<p>For example, assume you have a list of words that that look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>words = &#091;\"Messi\", \"is\", \"the\", \"best\", \"soccer\", \"player\"]<\/code><\/pre>\n\n\n\n<p>And you want to convert this into the string:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>\"Messi is the best soccer player\"<\/code><\/pre>\n\n\n\n<p>How can you do that?<\/p>\n\n\n\n<p>The correct way to achieve this is by using the <span class=\"symbol\">join<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> words = &#091;\"Messi\", \"is\", \"the\", \"best\", \"soccer\", \"player\"]\n>>> sentence = \" \".join(words)\n>>> sentence\n'Messi is the best soccer player'<\/code><\/pre>\n\n\n\n<p>Now that you know the answer, let me explain the join method in more detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The join Method<\/h2>\n\n\n\n<p><span class=\"symbol\">join()<\/span> is a method that is defined on the string class.<\/p>\n\n\n\n<p>The general syntax of join is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>str.join(iterable)<\/code><\/pre>\n\n\n\n<p>where:<\/p>\n\n\n\n<ul><li> <strong>str<\/strong> is a string representing the <em>separator<\/em> between the words to be joined.<\/li><li><strong>iterable<\/strong> could be any <em>iterable<\/em> (lists, tuples, string&#8230;). This iterable will include all the words to be joined. In this article, I will focus mainly on lists but the same concept applies to tuples, strings, and any iterable for that matter.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s take another example just to make sure everything is clear.<\/p>\n\n\n\n<p>Say you have a list of strings as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>fruits = &#091;\"apples\", \"bananas\", \"strawberries\"]<\/code><\/pre>\n\n\n\n<p>And you would like to convert the list into a single string with all the items in the list <em>separated by a comma<\/em>.<\/p>\n\n\n\n<p>In this case, the separator is the comma string <span class=\"symbol\">&#8220;, &#8220;<\/span> and the iterable is the <span class=\"symbol\">fruits<\/span> list.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> fruits = &#091;\"apples\", \"bananas\", \"strawberries\"]\n>>> csv = \", \".join(fruits)\n>>> csv\n'apples, bananas, strawberries'<\/code><\/pre>\n\n\n\n<p>Note that this only works if you have a list of strings.<\/p>\n\n\n\n<p>If your list has any item that is not a string, then you will get an error.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> words = &#091;\"Messi\", \"is\", \"number\", 1]\n>>> sentence = \" \".join(words)\nTypeError: sequence item 3: expected str instance, int found<\/code><\/pre>\n\n\n\n<p>So what do you do in a situation like this?<\/p>\n\n\n\n<p>Well, if your list has non-string items that could be converted to strings via the built-in <span class=\"symbol\">str()<\/span> function, then you can use the handy map method to <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/functions.html#map\" target=\"_blank\">map<\/a> all the items into a string first.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> words = &#091;\"Messi\", \"is\", \"number\", 1]\n>>> sentence = \" \".join(map(str, words))\n>>> sentence\n'Messi is number 1'<\/code><\/pre>\n\n\n\n<p>There you go.<\/p>\n\n\n\n<p>So next time you want to convert a list of strings into a single string, don&#8217;t create your own custom function to do that. Just use the Join method.<\/p>\n\n\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":2568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[554,1689,316,1688,160,242,1684,1685,1686,572,333,659,412,1691,1237,1692,1690,405,334,1683],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2562"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=2562"}],"version-history":[{"count":5,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions"}],"predecessor-version":[{"id":2567,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions\/2567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/2568"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=2562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=2562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=2562"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}