{"id":15882,"date":"2020-05-12T22:29:34","date_gmt":"2020-05-13T05:29:34","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=15882"},"modified":"2023-12-01T02:45:16","modified_gmt":"2023-12-01T10:45:16","slug":"python-join","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-join\/","title":{"rendered":"Python Join: A Guide"},"content":{"rendered":"\n<p>When you\u2019re working with lists in Python, you may decide that you want to merge the contents of a list into a string. For instance, you may want to merge a list of all employee names in a business into one single string.<br><\/p>\n\n\n\n<p>That\u2019s where the Python string <code>join()<\/code> method comes in. The <code>join()<\/code> method allows you to take an existing list and convert it into a single string.<br><\/p>\n\n\n\n<p>This tutorial will discuss, with examples, the basics of strings and how to use the Python <code>join()<\/code> method to convert lists into a single string.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python String: A Refresher<\/h2>\n\n\n\n<p>A string is a sequence of one or multiple characters. Strings are an essential data type in programming because they allow you to work with text data.<br><\/p>\n\n\n\n<p>In Python, strings are declared within either single (<code>\u2018\u2019<\/code>) or double (<code>\u201c\u201d<\/code>) quotes. Here\u2019s an example of a string:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>'This is a Python string, enclosed within single quotes.'<\/pre><\/div>\n\n\n\n<p>When you\u2019re working with a list in Python, you may want to convert it into a string. That\u2019s where the <code>join()<\/code> method can be useful.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python String join()<\/h2>\n\n\n\n<p>The <code>join()<\/code> string method allows you to merge together all the elements in a list and convert the result into a string. The <code>join()<\/code> method returns a string concatenated with the elements in a list.<br><\/p>\n\n\n\n<p>The syntax for the join string formatting method is as follows:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>separator.join(list_name)<\/pre><\/div>\n\n\n\n<p><code>separator<\/code> refers to the character(s) that should appear between each item in the newly-merged string, and <code>list_name<\/code> is the name of the list whose values you want to merge into a single string.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python String join() Example<\/h2>\n\n\n\n<p>Let\u2019s walk through an example to discuss how the built-in function <code>join()<\/code> works.<br><\/p>\n\n\n\n<p>Suppose we have a list that contains the names of all workers in a company\u2019s Sales Department. We want to concatenate the list into a string, whose values are separated with a comma. We could do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>employees = ['Donna Hanson', 'Anne Scott', 'Michael Parsons', 'Holly Harris']\nseparator = ','\nnew_employees = separator.join(employees)\nprint(new_employees)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Donna Hanson,Anne Scott,Michael Parsons,Holly Harris<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down our code, line-by-line. First, we declare a variable called \u201cemployees\u201d which stores the names of all four employees in the Sales Department. Then, we declare a variable called <code>separator<\/code> which stores the character we want to use to separate values in our newly-joined string later in our code.<br><\/p>\n\n\n\n<p>Then, we use the <code>join()<\/code> method to join together the contents of the \u201cemployees\u201d list, separated using the value of the <code>separator<\/code> variable (which, in this case, is a comma). Finally, we print the newly-joined string to the console.<br><\/p>\n\n\n\n<p>You can see that, instead of being a list, our names are stored as a string. Each value in our list is separated using a comma.<br><\/p>\n\n\n\n<p>Notice that the <code>join()<\/code> method does not automatically add a space between each value in the new string. If we want a space to appear between the values in our new string, we would have to specify one in our <code>separator<\/code>.<br><\/p>\n\n\n\n<p>The <code>join()<\/code> method also works with sets and tuples. Suppose we stored our employee names in a set (which is denoted using curly braces). We could convert the set into a string using the following same code as earlier, but instead of specifying a list, we can specify a set:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>employees = {'Donna Hanson', 'Anne Scott', 'Michael Parsons', 'Holly Harris'}\nseparator = ','\nnew_employees = separator.join(employees)\nprint(new_employees)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Donna Hanson,Anne Scott,Michael Parsons,Holly Harris<\/pre><\/div>\n\n\n\n<p>As you can see, the result of the <code>join()<\/code> method is the same as it was from our above example. The difference in our code is that, instead of using a list (denoted by <code>[]<\/code>), we use a set (denoted by <code>{}<\/code>). To use the <code>join()<\/code> method with a tuple, we would use curly brackets (<code>()<\/code>), which denote the set data type, instead of curly braces (<code>{}<\/code>) as we did in this example.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Python <code>join()<\/code> method allows you to merge the contents of a list into a string, and add a separator between each value in the new string.<br><\/p>\n\n\n\n<p>This tutorial discussed, with reference to examples, how to use the Python <code>join()<\/code> method to join together the contents of a list. Now you\u2019re ready to start using the <code>join()<\/code> method like a Python professional!<\/p>\n","protected":false},"excerpt":{"rendered":"When you\u2019re working with lists in Python, you may decide that you want to merge the contents of a list into a string. For instance, you may want to merge a list of all employee names in a business into one single string. That\u2019s where the Python string join() method comes in. The join() method&hellip;","protected":false},"author":240,"featured_media":15883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-15882","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Join: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-join\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Join: A Guide\" \/>\n<meta property=\"og:description\" content=\"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-join\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-13T05:29:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:45:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Join: A Guide\",\"datePublished\":\"2020-05-13T05:29:34+00:00\",\"dateModified\":\"2023-12-01T10:45:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/\"},\"wordCount\":645,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-join\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-join\/\",\"name\":\"Python Join: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg\",\"datePublished\":\"2020-05-13T05:29:34+00:00\",\"dateModified\":\"2023-12-01T10:45:16+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-join\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-join\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Join: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Join: A Guide | Career Karma","description":"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.","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:\/\/careerkarma.com\/blog\/python-join\/","og_locale":"en_US","og_type":"article","og_title":"Python Join: A Guide","og_description":"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.","og_url":"https:\/\/careerkarma.com\/blog\/python-join\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-13T05:29:34+00:00","article_modified_time":"2023-12-01T10:45:16+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-join\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Join: A Guide","datePublished":"2020-05-13T05:29:34+00:00","dateModified":"2023-12-01T10:45:16+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/"},"wordCount":645,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-join\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-join\/","url":"https:\/\/careerkarma.com\/blog\/python-join\/","name":"Python Join: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg","datePublished":"2020-05-13T05:29:34+00:00","dateModified":"2023-12-01T10:45:16+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Python string join() method merges the contents of an iterable object and returns a string. On Career Karma, learn how to use the join() method.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-join\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-join\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-join\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-bottle-bright-building-273238.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-join\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Join: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=15882"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15882\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15883"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=15882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=15882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=15882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}