{"id":7584,"date":"2024-03-19T06:50:25","date_gmt":"2024-03-19T06:50:25","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7584"},"modified":"2024-03-19T06:50:25","modified_gmt":"2024-03-19T06:50:25","slug":"python-string-concatenation","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-string-concatenation\/","title":{"rendered":"Python String Concatenation"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-string-concatenation\">What is Python String Concatenation?<\/a><\/li><li ><a href=\"#what-are-the-different-ways-to-concatenate-strings\">What are the different ways to concatenate strings?<\/a><\/li><li ><a href=\"#1-string-concatenation-using-operator\">1. String Concatenation using &#8216;+&#8217; Operator<\/a><\/li><li ><a href=\"#2-string-concatenation-using-join-method\">2. String Concatenation using join() Method<\/a><\/li><li ><a href=\"#3-string-concatenation-using-operator\">3. String Concatenation using &#8216;%&#8217; Operator<\/a><\/li><li ><a href=\"#4-string-concatenation-using-the-format-function\">4. String Concatenation using the  format() function<\/a><\/li><li ><a href=\"#5-string-concatenation-using-comma\">5. String Concatenation using \u201c, \u201d comma<\/a><\/li><li ><a href=\"#6-string-concatenation-using-f-string\">6. String Concatenation using f-string<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-string-concatenation-fa-qs\">Python String Concatenation- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>In Python, it is possible to concatenate two different strings, even with the same string to itself multiple times using the + and the * operator. Read this article to learn more about Python String Concatenation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-string-concatenation\">What is Python String Concatenation?<\/h2>\n\n\n\n<p>The + operator is an addition operator and will return the sum of two numbers. Hence, the + symbol will act as a string concatenation operator in <a href=\"https:\/\/www.skillvertex.com\/blog\/python-tutorial\/\" data-type=\"post\" data-id=\"6852\">Python<\/a>. Thus, it will function with two <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-reverse-a-string-in-java\/\" data-type=\"post\" data-id=\"728\">string <\/a>operands and get the end output as the concatenation of the two. <\/p>\n\n\n\n<p>Hence, the characters of the string on the right of the plus symbol will appear at the left of its string. The result of the concatenation will be considered as the new string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-different-ways-to-concatenate-strings\">What are the different ways to concatenate strings?<\/h2>\n\n\n\n<p>Python String Concatenation will be considered as the method of adding two strings. Check out the different methods to concatenate strings below:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using + operator<\/li>\n\n\n\n<li>Using join() method<\/li>\n\n\n\n<li>Using % operator<\/li>\n\n\n\n<li>Using format() function<\/li>\n\n\n\n<li>Using &#8220;,&#8221; (comma)<\/li>\n\n\n\n<li>Using f-string (Literal String Interpolation)<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-string-concatenation-using-operator\">1. String Concatenation using &#8216;+&#8217; Operator<\/h2>\n\n\n\n<p>In Python, when you join two strings using the <code>+<\/code> sign, it&#8217;s called string concatenation. It&#8217;s like combining two pieces of text. However, you need to remember that strings, which are sequences of characters, cannot be changed directly. Whereas, if this operator is used on integers, then it will perform the mathematical addition.<\/p>\n\n\n\n<p>Example 1<\/p>\n\n\n\n<p>In this example, var1 and var2 will be stored in another variable such as var3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Defining strings\nvar1 = \"Hello \"\nvar2 = \"Skillvertex\"\n \n# + Operator is used to combine strings\nvar3 = var1 + var2\nprint(var3)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-string-concatenation-using-join-method\">2. String Concatenation using join() Method<\/h2>\n\n\n\n<p>The join() Method will be considered a String Method. Thus,  will return the string where the elements of the sequence will be joined by the string operator. <\/p>\n\n\n\n<p>Therefore, it will accept only the list as its <a href=\"https:\/\/www.skillvertex.com\/blog\/python-keyword-arguments\/\" data-type=\"post\" data-id=\"7283\">argument<\/a>, and the list size will be anything. The example below illustrates the Python concatenate string with the join() operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1 = \"Skill\"\nvar2 = \"vertex\"\n \n# join() method is used to combine the strings\nprint(\"\".join(&#91;var1, var2]))\n \n# join() method is used here to combine \n# the string with a separator Space(\" \")\nvar3 = \" \".join(&#91;var1, var2])\n \nprint(var3)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skillvertex\nSkillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-string-concatenation-using-operator\">3. String Concatenation using &#8216;%&#8217; Operator<\/h2>\n\n\n\n<p>The % Operator will work for <a href=\"https:\/\/www.skillvertex.com\/blog\/python-string-formatting\/\" data-type=\"post\" data-id=\"7587\">string formatting<\/a>. Additionally, it will be used as a string concatenation.  This operator is beneficial when you need to concatenate strings and will do simple formatting.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1 = \"Welcome\"\nvar2 = \"Skillvertex\"\n \n# % Operator is used here to combine the string\nprint(\"% s % s\" % (var1, var2))<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-string-concatenation-using-the-format-function\">4. String Concatenation using the  format() function<\/h2>\n\n\n\n<p>The str, format will be considered as the string formatting method where they provide multiple substitutions and value formatting. Hence, it can concatenate the elements within the string through positional formatting.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1 = \"Hello\"\nvar2 = \"Skillvertex\"\n \n# format function is used here to \n# combine the string\nprint(\"{} {}\".format(var1, var2))\n \n# store the result in another variable \nvar3 = \"{} {}\".format(var1, var2)\n \nprint(var3)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex\nHello Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-string-concatenation-using-comma\">5. String Concatenation using \u201c, \u201d comma<\/h2>\n\n\n\n<p>A comma will be considered as a great alternative to the string concatenation with the + operator. Thus, it is required to use the comma for combining the data types with the single whitespace in between.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1 = \"Skill\"\nvar2 = \"Vertex\"\nvar3 = \".com\"\n \n# using comma to combine data types\n# with a single whitespace.\nprint(var1, var2, var3)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skillvertex.com<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-string-concatenation-using-f-string\">6. String Concatenation using f-string<\/h2>\n\n\n\n<p>Using the F-string for the string concatenation will be operated on the Python versions above 3.6 and will be introduced in the PEP 498- Literal String Interpolation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Variables\nvar1 = \"Skill\"\nvar2 = \"Vertex\"\nvar3 = \".com\"\n\n# String concatenation using f-string\nresult = f\"{var1} {var2} {var3}\"\n\n# Printing the result\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill Vertex .com\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In Python, string concatenation is the process of combining multiple strings into a single string. The <code>+<\/code> operator is commonly used for this purpose. When concatenating strings, it&#8217;s important to remember that strings are immutable, meaning the original strings are not modified; instead, a new string is created. <\/p>\n\n\n\n<p>Additionally, Python offers convenient f-strings (formatted string literals) for concise and readable string concatenation, allowing variables to be easily inserted into strings. This straightforward process of joining strings provides flexibility in constructing meaningful and dynamic text outputs in Python programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-string-concatenation-fa-qs\">Python String Concatenation- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709535862572\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What symbol is used to concatenate 2 strings in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The + operator will combine the 2 strings.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709535870808\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What functions concatenate two strings?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, the\u00a0<strong>strcat()<\/strong>\u00a0function will operate to concatenate on the two strings.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709535877844\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.How do I concatenate two string columns in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.df[&#8220;full_name&#8221;] = df[&#8220;first_name&#8221;] + &#8220;+df[&#8220;last_name&#8221;]<br \/>df[&#8220;full_name&#8221;] = df[[&#8220;first_name&#8221;,&#8221;last_name&#8221;]].agg(&#8221; &#8220;.join, axis=1)<br \/>df = df.withColumn( &#8220;full_name&#8221;, F.concat(&#8220;first_name&#8221;, F.lit(&#8221; &#8220;), &#8220;last_name&#8221;) )<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In Python, it is possible to concatenate two different strings, even with the same string to itself multiple times using the + and the * operator. Read this article to learn more about Python String Concatenation. What is Python String Concatenation? The + operator is an addition operator and will return the sum of two &#8230; <a title=\"Python String Concatenation\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-string-concatenation\/\" aria-label=\"More on Python String Concatenation\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7585,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[892,955,958,866],"class_list":["post-7584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-operator","tag-python-string","tag-python-string-concatenation","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7584"}],"version-history":[{"count":5,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7584\/revisions"}],"predecessor-version":[{"id":8285,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7584\/revisions\/8285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7585"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}