{"id":4191,"date":"2020-03-23T07:28:02","date_gmt":"2020-03-23T07:28:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4191"},"modified":"2023-02-16T19:57:12","modified_gmt":"2023-02-16T19:57:12","slug":"python-string-join-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-join-method","title":{"rendered":"Python String join() function"},"content":{"rendered":"\n<p>In this article, we will have a look at the <strong>Python String join() function<\/strong>. As the name suggests, it is used to join strings together and works for the data of string type.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Python string join() method<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/string\" class=\"rank-math-link\">Python String<\/a> has various in-built functions to deal with the string type of data. <\/p>\n\n\n\n<p>The <code>join()<\/code> method basically is used to <strong>join the input string by another set of separator\/string elements<\/strong>. It accepts <strong>iterables<\/strong> such as <a href=\"https:\/\/www.askpython.com\/python\/set\" class=\"rank-math-link\">set<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">list<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">tuple<\/a>, string, etc and another <strong>string<\/strong>(separable element) as parameters. <\/p>\n\n\n\n<p>The join() function returns a string that <strong>joins the elements of the iterable with the separator string<\/strong> passed as an argument to the function.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nseparator-string.join(iterable)\n<\/pre><\/div>\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str=&#039;JournalDev&#039;\ninsert_str=&#039;*&#039;\nres=insert_str.join(inp_str)\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nJ*o*u*r*n*a*l*D*e*v\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str=&#039;PYTHON&#039;\ninsert_str=&#039;#!&#039;\nres=insert_str.join(inp_str)\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nP#!Y#!T#!H#!O#!N\n<\/pre><\/div>\n\n\n<p>Hey, Folks! The most important point to be taken into consideration is that the <strong>join() function operates only on string type input values<\/strong>. If we input any of the parameters of <strong>non-string type<\/strong>, it raises a <code>TypeError exception<\/code>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str=200  #non-string type input\ninsert_str=&#039;S&#039; \nres=insert_str.join(inp_str)\nprint(res)\n<\/pre><\/div>\n\n\n<p>In the above example, the separator string i.e. insert_str has been assigned an integer value. Thus, it would raise a TypeError exception.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n---------------------------------------------------------------------------\nTypeError                                 Traceback (most recent call last)\n&lt;ipython-input-11-ef2dcbcf6abf&gt; in &lt;module&gt;\n      1 inp_str=200  #non-string type input\n      2 insert_str=&#039;S&#039;\n----&gt; 3 res=insert_str.join(inp_str)\n      4 print(res)\n\nTypeError: can only join an iterable\n<\/pre><\/div>\n\n\n<p><strong>Python string join() method with <\/strong><code>list<\/code><strong> as an iterable<\/strong>:<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nseparator-string.join(list)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_lst=&#x5B;&#039;10&#039;,&#039;20&#039;,&#039;30&#039;,&#039;40&#039;]\nsep=&#039;@@&#039;\nres=sep.join(inp_lst)\nprint(res)\n<\/pre><\/div>\n\n\n<p>In the above example, the separator string &#8220;@@&#8221; gets joined to every element of the input list i.e. inp_lst.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n10@@20@@30@@40\n<\/pre><\/div>\n\n\n<p><strong>Python join() method with <\/strong><code>set<\/code><strong> an iterable<\/strong>:<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nseparator-string.join(set)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_set=(&#039;10&#039;,&#039;20&#039;,&#039;30&#039;,&#039;40&#039;)\nsep=&#039;**&#039;\nsep1=&#039;&lt;&#039;\nres=sep.join(inp_set)\nprint(res)\nres1=sep1.join(inp_set)\nprint(res1)\n<\/pre><\/div>\n\n\n<p>In the above example, the separator string &#8220;**&#8221; and &#8220;&lt;&#8221; gets joined to each element of the input set.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n10**20**30**40\n10&lt;20&lt;30&lt;40\n<\/pre><\/div>\n\n\n<p><strong>Python join() method with <\/strong><code>dictionary<\/code><strong> <\/strong><strong>as an iterable:<\/strong><\/p>\n\n\n\n<p>Python string join() method can also be applied to the <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" class=\"rank-math-link\">dictionary <\/a>as an iterable. <\/p>\n\n\n\n<p>But, the important thing to note is that the <strong>join() method works only on the keys of the dictionary<\/strong> data structure and not the values associated with the keys.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nseparator-string.join(dict)\n<\/pre><\/div>\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_dict={&#039;Python&#039;:&#039;1&#039;,&#039;Java&#039;:&#039;2&#039;,&#039;C++&#039;:&#039;3&#039;}\nsep=&#039;##&#039;\nres=sep.join(inp_dict)\nprint(res)\n<\/pre><\/div>\n\n\n<p>As seen in the above example, the join() method only considers the keys of the dict for manipulation. It completely neglects the values of the dict.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPython##Java##C++\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_dict={&#039;Python&#039;:1,&#039;Java&#039;:2,&#039;C++&#039;:3}\nsep=&#039;##&#039;\nres=sep.join(inp_dict)\nprint(res)\n<\/pre><\/div>\n\n\n<p>In the above example, the values in the dict are of non-string type. Still, it would cause no error to the execution of the code because join() method deals only with the keys of the dictionary.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPython##Java##C++\n<\/pre><\/div>\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_dict={1:&#039;Python&#039;,2:&#039;Java&#039;,3:&#039;C++&#039;}\nsep=&#039;##&#039;\nres=sep.join(inp_dict)\nprint(res)\n<\/pre><\/div>\n\n\n<p>The above code returns a <strong>TypeError<\/strong> because, the key values associated with the dictionary are of non-string type.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTypeError                                 Traceback (most recent call last)\n&lt;ipython-input-34-bb7356c41bc8&gt; in &lt;module&gt;\n      1 inp_dict={1:&#039;Python&#039;,2:&#039;Java&#039;,3:&#039;C++&#039;}\n      2 sep=&#039;##&#039;\n----&gt; 3 res=sep.join(inp_dict)\n      4 print(res)\n\nTypeError: sequence item 0: expected str instance, int found\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python numpy.join() method<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" class=\"rank-math-link\">Python NumPy module<\/a> has got in-built functions to deal with the string data in an array. <\/p>\n\n\n\n<p>The <code>numpy.core.defchararray.join(sep-string,inp-arr)<\/code> is used to join the elements of the array with the passed separator string as an argument. <\/p>\n\n\n\n<p>It accepts an array containing string type elements and separator string as arguments and <strong>returns an array containing elements separated by the input separator string (delimiter)<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.core.defchararray.join(separator-string,array)\n<\/pre><\/div>\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ninp_arr=np.array(&#x5B;&quot;Python&quot;,&quot;Java&quot;,&quot;Ruby&quot;,&quot;Kotlin&quot;])\nsep=np.array(&quot;**&quot;)\nres=np.core.defchararray.join(sep,inp_arr)\nprint(res)\n\n<\/pre><\/div>\n\n\n<p>In the above example, we have generated an array out of the passed list elements using <code>numpy.array()<\/code> method. Further, by using the join() function, it joins the string &#8220;**&#8221; to every element of the array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;P**y**t**h**o**n&#039; &#039;J**a**v**a&#039; &#039;R**u**b**y&#039; &#039;K**o**t**l**i**n&#039;]\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ninp_arr=np.array(&#x5B;&quot;Python&quot;,&quot;Java&quot;,&quot;Ruby&quot;,&quot;Kotlin&quot;])\nsep=np.array(&#x5B;&quot;**&quot;,&quot;++&quot;,&quot;&amp;&amp;&quot;,&quot;$$&quot;])\nres=np.core.defchararray.join(sep,inp_arr)\nprint(res)\n\n<\/pre><\/div>\n\n\n<p>In the above example, we have used a different separate string for every element of the array. The only condition stays that the count of the separable string(delimiter) in the array should match with the number of elements in the input array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;P**y**t**h**o**n&#039; &#039;J++a++v++a&#039; &#039;R&amp;&amp;u&amp;&amp;b&amp;&amp;y&#039; &#039;K$$o$$t$$l$$i$$n&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Pandas str.join() method<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" class=\"rank-math-link\">Python Pandas module<\/a> has in-built pandas.str.join() method to join the elements of the data-set with the provided delimiter.<\/p>\n\n\n\n<p>The <code>pandas.str.join()<\/code> method works on the particular column(data) values of the data set or input series and returns the series with joined data items with the separator string or delimiter.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSeries.str.join(delimiter or separator-string)\n<\/pre><\/div>\n\n\n<p><strong>Input .csv file: Book1.csv<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"173\" height=\"317\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/Input-csv-file-Book1.png\" alt=\"Input csv file-Book1\" class=\"wp-image-4199\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/Input-csv-file-Book1.png 173w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/Input-csv-file-Book1-164x300.png 164w\" sizes=\"auto, (max-width: 173px) 100vw, 173px\" \/><figcaption><strong>Input csv file-Book1<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas\ninfo=pandas.read_csv(&quot;C:\\\\Book1.csv&quot;)\ninfo&#x5B;&quot;Name&quot;]=info&#x5B;&quot;Name&quot;].str.join(&quot;||&quot;)\nprint(info)\n<\/pre><\/div>\n\n\n<p>In the above example, we have used <code>pandas.read_csv()<\/code> method to read the contents of the data set. Further, we join the separator string i.e &#8220;||&#8221; to the data values of the column &#8220;Name&#8221; of the input data set.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n           Name  Age\n0        J||i||m   21\n1  J||e||n||n||y   22\n2     B||r||a||n   24\n3  S||h||a||w||n   12\n4  R||i||t||i||k   26\n5     R||o||s||y   24\n6  D||a||n||n||y   25\n7  D||a||i||s||y   15\n8        T||o||m   27\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The join() method is used to <strong>join elements or iterables of string type with a string delimiter<\/strong> element.<\/li><li>It is mandatory for the arguments: iterable elements and the delimiter to be of <code>string type<\/code>.<\/li><li>Moreover, Python join() method can also be applied to iterables such as a set, list, dict, etc. <\/li><li>The join() method raises a <code>TypeError exception<\/code> if the delimiter or the input iterable contains elements of non-string type.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the working of Python String join() method with different iterables such as a set, list, tuple, dict, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python String join() method &#8211; JournalDev<\/li><li>Python String functions<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will have a look at the Python String join() function. As the name suggests, it is used to join strings together and works for the data of string type. Understanding Python string join() method Python String has various in-built functions to deal with the string type of data. The join() method [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":4213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4191","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\/4191","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4191"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4191\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4213"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}