{"id":2560,"date":"2020-01-13T20:16:36","date_gmt":"2020-01-13T20:16:36","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2560"},"modified":"2022-08-06T13:17:32","modified_gmt":"2022-08-06T13:17:32","slug":"python-concatenate-string-and-int","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-concatenate-string-and-int","title":{"rendered":"Python &#8211; Concatenate string and int"},"content":{"rendered":"\n<p>In Python, we normally perform string concatenation using the <code>+<\/code> operator. The <code>+<\/code> operator, however as we know, is also used to add integers or floating-point numbers.<\/p>\n\n\n\n<p>So what would happen if we have a <em>string<\/em> and an <em>int<\/em> on both sides of the operand?<\/p>\n\n\n\n<p>Since Python is a dynamically typed language, we would not face any error during compilation, but rather, we get a <strong>Runtime Error<\/strong>. (More specifically, a <strong>TypeError<\/strong> exception is raised)<\/p>\n\n\n\n<p>The below snippet proves this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello, I am in grade &quot;\n\nb = 12\n\nprint(a + b)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTraceback (most recent call last):\n  File &quot;concat.py&quot;, line 5, in &lt;module&gt;\n    print(a + b)\nTypeError: can only concatenate str (not &quot;int&quot;) to str\n<\/pre><\/div>\n\n\n<p>So, since we cannot directly concatenate an integer with a string, we need to manipulate the operands so that they can be concatenated. There are multiple ways of doing this.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Using str()<\/h2>\n\n\n\n<p>We can convert the integer to a string, via the <code>str()<\/code> function. Now, the new string can now be concatenated with the other string to give the Output;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(a + str(b))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHello, I am in grade 12\n<\/pre><\/div>\n\n\n<p>This is the most common way to convert an integer to a string.<\/p>\n\n\n\n<p>But, we can use other methods as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Using format()<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello, I am in grade &quot;\n\nb = 12\n\nprint(&quot;{}{}&quot;.format(a, b))\n<\/pre><\/div>\n\n\n<p>The output remains the same as before.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Using &#8216;%&#8217; format specifier<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello, I am in grade &quot;\n\nb = 12\n\nprint(&quot;%s%s&quot; % (a, b))\n<\/pre><\/div>\n\n\n<p>While we can specify that both <code>a<\/code> and <code>b<\/code> are strings, we can also use C-style format specifiers (<code>%d<\/code>, <code>%s<\/code>) to concatenate an integer with a string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;%s%d&#039; % (a,b))\n<\/pre><\/div>\n\n\n<p>The output remains the same for the above code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Using f-strings<\/h2>\n\n\n\n<p>We can use Python f-strings on Python 3.6 or above to concatenate an integer with a string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello, I am in grade &quot;\n\nb = 12\n\nprint(f&quot;{a}{b}&quot;)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">5. Printing the string using print()<\/h2>\n\n\n\n<p>If we want to directly print the concatenated string, we can use <code>print()<\/code> to do the concatenation for us.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello, I am in grade &quot;\nb = 12\nprint(a, b, sep=&quot;&quot;)\n<\/pre><\/div>\n\n\n<p>We join <code>a<\/code> and <code>b<\/code> using a null string separator (<code>sep<\/code>), since the default separator for <code>print()<\/code> is a space (&#8221; &#8220;).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how to concatenate an integer to a string using various methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/25675943\/how-can-i-concatenate-str-and-int-objects\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">StackOverflow Question on Concatenation of string and int<\/a><\/li><li>JournalDev article on Concatenation of string and int<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In Python, we normally perform string concatenation using the + operator. The + operator, however as we know, is also used to add integers or floating-point numbers. So what would happen if we have a string and an int on both sides of the operand? Since Python is a dynamically typed language, we would not [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2560","post","type-post","status-publish","format-standard","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2560","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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2560"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2560\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}