{"id":958,"date":"2020-03-13T10:14:41","date_gmt":"2020-03-13T17:14:41","guid":{"rendered":"https:\/\/renanmf.com\/?p=958"},"modified":"2021-03-21T15:37:49","modified_gmt":"2021-03-21T18:37:49","slug":"join-items-iterable-into-string-python","status":"publish","type":"post","link":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/","title":{"rendered":"join(): join items of an iterable into one string in Python"},"content":{"rendered":"<p>Use the <code>join()<\/code> method to join all the items if an iterable into a string.<\/p>\n<p>The basic syntax is: <code>string.join(iterable)<\/code><\/p>\n<p>As per the syntax above, a string is required as a separator.<\/p>\n<p>The method returns a new string, which means that the original iterator remains unchanged.<\/p>\n<p>Since the <code>join()<\/code> method only accepts strings, if any element in the iterable is of a different type, an error will be thrown.<\/p>\n<p>Let&#8217;s see some examples with: string, list, tuple, set, and dictionary<\/p>\n<h2>join(): Strings<\/h2>\n<p>The <code>join()<\/code> method puts the <code>$<\/code> sign as a separator for every character in the string.<\/p>\n<pre><code class=\"language-python\">my_string = &#039;beach&#039;\n\nprint(&#039;$&#039;.join(my_string))\n#output: b$e$a$c$h<\/code><\/pre>\n<h2>join(): Lists<\/h2>\n<p>I have a simple list of three items representing car brands.<\/p>\n<p>The <code>join()<\/code> method is gonna use the <code>$<\/code> sign as a separator.<\/p>\n<p>It concatenates all the items on the list and puts the <code>$<\/code> sign between them.<\/p>\n<pre><code class=\"language-python\">my_list = [&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;]\n\nprint(&#039;$&#039;.join(my_list))\n#output: bmw$ferrari$mclaren<\/code><\/pre>\n<p>This another example remembers you that <code>join()<\/code> does not work with non-string items.<\/p>\n<p>When trying to concatenate the <code>int<\/code> items, an error is raised.<\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3]\n\nprint(&#039;$&#039;.join(my_list))\n#output:\n#Traceback (most recent call last):\n#  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\n#TypeError: sequence item 0: expected str instance, int found<\/code><\/pre>\n<h2>join(): Tuples<\/h2>\n<p>The tuple follows the same rationale as the list example explained before.<\/p>\n<p>Again, I&#8217;m using the <code>$<\/code> sign as separator.<\/p>\n<pre><code class=\"language-python\">my_tuple = (&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;)\n\nprint(&#039;$&#039;.join(my_tuple))\n#output: bmw$ferrari$mclaren<\/code><\/pre>\n<h2>join(): Sets<\/h2>\n<p>Since the set is also the same as the tuple and the list, I&#8217;ve used a different separator in this example.<\/p>\n<pre><code class=\"language-python\">my_set = {&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;}\nprint(&#039;|&#039;.join(my_set))\n#output: ferrari|bmw|mclaren<\/code><\/pre>\n<h2>join(): dictionaries<\/h2>\n<p>The dictionary has a catch when you use the <code>join()<\/code> method: it joins the keys, not the values.<\/p>\n<p>This example shows the concatenation of the keys.<\/p>\n<pre><code class=\"language-python\">my_dict = {&#039;bmw&#039;: &#039;BMW I8&#039;, &#039;ferrari&#039;: &#039;Ferrari F8&#039;, &#039;mclaren&#039;: &#039;McLaren 720S&#039;}\n\nprint(&#039;,&#039;.join(my_dict))\n#output: bmw,ferrari,mclaren<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use the join() method to join all the items if an iterable into a string. The basic syntax is: string.join(iterable) As per the syntax above, a string is required as a separator. The method returns a new string, which means that the original iterator remains unchanged. Since the join() method only accepts strings, if any [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":742,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6],"class_list":["post-958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>join(): join items of an iterable into one string in Python<\/title>\n<meta name=\"description\" content=\"Learn how to use the join() method to join all the items if an iterable into a string.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"join(): join items of an iterable into one string in Python\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the join() method to join all the items if an iterable into a string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Renan Moura - Software Engineering\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/renanmouraf\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-13T17:14:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-21T18:37:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Renan Moura\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/renanmouraf\" \/>\n<meta name=\"twitter:site\" content=\"@renanmouraf\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Renan Moura\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/\"},\"author\":{\"name\":\"Renan Moura\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\"},\"headline\":\"join(): join items of an iterable into one string in Python\",\"datePublished\":\"2020-03-13T17:14:41+00:00\",\"dateModified\":\"2021-03-21T18:37:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/\"},\"wordCount\":237,\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/\",\"name\":\"join(): join items of an iterable into one string in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"datePublished\":\"2020-03-13T17:14:41+00:00\",\"dateModified\":\"2021-03-21T18:37:49+00:00\",\"description\":\"Learn how to use the join() method to join all the items if an iterable into a string.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"width\":1800,\"height\":1200,\"caption\":\"how to python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/join-items-iterable-into-string-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/renanmf.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"join(): join items of an iterable into one string in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"name\":\"Renan Moura - Software Engineering\",\"description\":\"Software development, machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/renanmf.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\",\"name\":\"Renan Moura\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"width\":120,\"height\":120,\"caption\":\"Renan Moura\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/renanmouraf\",\"https:\\\/\\\/x.com\\\/renanmouraf\",\"https:\\\/\\\/instagram.com\\\/renanmouraf\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\",\"name\":\"Renan Moura\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"caption\":\"Renan Moura\"},\"description\":\"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.\",\"sameAs\":[\"https:\\\/\\\/renanmf.com\\\/\",\"https:\\\/\\\/www.instagram.com\\\/renanmouraf\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/renanmouraf\"],\"url\":\"https:\\\/\\\/renanmf.com\\\/author\\\/renanmoura\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"join(): join items of an iterable into one string in Python","description":"Learn how to use the join() method to join all the items if an iterable into a string.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/","og_locale":"en_US","og_type":"article","og_title":"join(): join items of an iterable into one string in Python","og_description":"Learn how to use the join() method to join all the items if an iterable into a string.","og_url":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/","og_site_name":"Renan Moura - Software Engineering","article_publisher":"https:\/\/www.facebook.com\/renanmouraf","article_published_time":"2020-03-13T17:14:41+00:00","article_modified_time":"2021-03-21T18:37:49+00:00","og_image":[{"width":1800,"height":1200,"url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","type":"image\/jpeg"}],"author":"Renan Moura","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/renanmouraf","twitter_site":"@renanmouraf","twitter_misc":{"Written by":"Renan Moura","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#article","isPartOf":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/"},"author":{"name":"Renan Moura","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8"},"headline":"join(): join items of an iterable into one string in Python","datePublished":"2020-03-13T17:14:41+00:00","dateModified":"2021-03-21T18:37:49+00:00","mainEntityOfPage":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/"},"wordCount":237,"publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"image":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/","url":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/","name":"join(): join items of an iterable into one string in Python","isPartOf":{"@id":"https:\/\/renanmf.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#primaryimage"},"image":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","datePublished":"2020-03-13T17:14:41+00:00","dateModified":"2021-03-21T18:37:49+00:00","description":"Learn how to use the join() method to join all the items if an iterable into a string.","breadcrumb":{"@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/renanmf.com\/join-items-iterable-into-string-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#primaryimage","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","width":1800,"height":1200,"caption":"how to python"},{"@type":"BreadcrumbList","@id":"https:\/\/renanmf.com\/join-items-iterable-into-string-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/renanmf.com\/"},{"@type":"ListItem","position":2,"name":"join(): join items of an iterable into one string in Python"}]},{"@type":"WebSite","@id":"https:\/\/renanmf.com\/#website","url":"https:\/\/renanmf.com\/","name":"Renan Moura - Software Engineering","description":"Software development, machine learning","publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/renanmf.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/renanmf.com\/#organization","name":"Renan Moura","url":"https:\/\/renanmf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","width":120,"height":120,"caption":"Renan Moura"},"image":{"@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/renanmouraf","https:\/\/x.com\/renanmouraf","https:\/\/instagram.com\/renanmouraf","https:\/\/www.linkedin.com\/in\/renanmouraf\/"]},{"@type":"Person","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8","name":"Renan Moura","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","caption":"Renan Moura"},"description":"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.","sameAs":["https:\/\/renanmf.com\/","https:\/\/www.instagram.com\/renanmouraf\/","https:\/\/www.linkedin.com\/in\/renanmouraf\/","https:\/\/x.com\/https:\/\/twitter.com\/renanmouraf"],"url":"https:\/\/renanmf.com\/author\/renanmoura\/"}]}},"_links":{"self":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/comments?post=958"}],"version-history":[{"count":4,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/958\/revisions"}],"predecessor-version":[{"id":3240,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/958\/revisions\/3240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media\/742"}],"wp:attachment":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media?parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/categories?post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/tags?post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}