{"id":12650,"date":"2021-02-17T15:24:09","date_gmt":"2021-02-17T15:24:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=12650"},"modified":"2023-04-07T12:50:52","modified_gmt":"2023-04-07T12:50:52","slug":"semicolon-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/semicolon-in-python","title":{"rendered":"Semicolon in Python"},"content":{"rendered":"\n<p>The common meaning of a semicolon(;) in various programming languages is to end or discontinue the current statement. In programming languages such as C, C++, and Java, it is necessary to use a semicolon to end a line of code. However, this is not the case with Python.<\/p>\n\n\n\n<p>A semicolon in Python signifies separation rather than termination. It allows you to write multiple statements on a single line.<\/p>\n\n\n\n<p>There are many use cases of semicolons than just mentioned above, in this tutorial we will see different uses of semicolons in Python and understand it better with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why are semicolons allowed in Python?<\/h2>\n\n\n\n<p>Python does not&nbsp;require&nbsp;semi-colons to terminate statements. Semicolons&nbsp;can&nbsp;be used to delimit statements if you wish to put multiple statements on the same line. <\/p>\n\n\n\n<p>A&nbsp;<strong>semicolon<\/strong>&nbsp;in&nbsp;<strong>Python<\/strong>&nbsp;denotes separation, rather than termination. It allows you to write multiple statements on the same line. This syntax also makes it&nbsp;<strong>legal<\/strong>&nbsp;to put a&nbsp;<strong>semicolon<\/strong>&nbsp;at the end of a single statement. So, it&#8217;s actually two statements and the second one is empty. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to print a semicolon in Python?<\/h2>\n\n\n\n<p>Many people consider the semicolon to be different from other alphabets. Let&#8217;s see what happens when we try to print a semicolon as a regular <a href=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\" rel=\"noreferrer noopener\">string <\/a>in Python<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; print(&quot;;&quot;)\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=\"\">\n;\n<\/pre><\/div>\n\n\n<p>It treats the semicolon no differently and prints it out. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Split Statements with Semicolons<\/h2>\n\n\n\n<p>Sometimes there is a need to write multiple statements in a line to make the code single liners or for some other reason, in this type of case semicolons are very useful. A semicolon can be used to separate statements in Python.<\/p>\n\n\n\n<p>Below is the syntax for using a semicolon to separate two statements, but these statements can be more than two.<\/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=\"\">\nstatement1; statement2\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In this example, we will try to put more than 2 statements in a single line with the use of the semicolon.<\/p>\n\n\n\n<p>Here are the three statements in Python without semicolons:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; print(&#039;Hi&#039;)\n&gt;&gt;&gt; print(&#039;Hello&#039;)\n&gt;&gt;&gt; print(&#039;Hola!&#039;)\n<\/pre><\/div>\n\n\n<p>Now let&#8217;s use the same three statements with semicolons<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;Hi&#039;); print(&#039;Hello&#039;); print(&#039;Hola!&#039;)\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=\"\">\nHi\nHello\nHola!\n<\/pre><\/div>\n\n\n<p>As you can see, Python executes the three statements individually after we split them with semicolons. Without the use of that, the interpreter would give us an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Semicolons with Loops in Python<\/h2>\n\n\n\n<p>In loops like the <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\">For loop<\/a>, a semicolon can be used if the whole statement starts with a loop. You use a semicolon to form a coherent statement like the body of the loop.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In this example, we&#8217;ll try to loop through two statements separated by a semicolon and see if the loop prints both or just the first one.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range (4): print (&#039;Hi&#039;) ; print(&#039;Hello&#039;)\n<\/pre><\/div>\n\n\n<p>Here we have used the<a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-range-method\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-range-method\" rel=\"noreferrer noopener\"> range() method<\/a> to specify a loop to execute statements four times.<\/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=\"\">\nHi\nHello\nHi\nHello\nHi\nHello\nHi\nHello\n<\/pre><\/div>\n\n\n<p>In the above output, we can see that it successfully prints both statements the specified number of times, which means it can loop through all the statements separated by semicolons.<\/p>\n\n\n\n<p><strong>Python will throw an error if you use a semicolon to separate a normal expression from a block statement i.e loop.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;Hi&#039;) ; for i in range (4): print (&#039;Hello&#039;)\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=\"\">\nInvalid Syntax\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned various use cases of a semicolon in Python. Let&#8217;s summarize them with two pointers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A semicolon in Python is mostly used to separate multiple statements written on a single line.<\/li>\n\n\n\n<li>A semicolon is used to write a minor statement and reserve a bit of space like name = Marie; age = 23; print(name, age)<\/li>\n<\/ul>\n\n\n\n<p>The use of semicolons is very &#8220;non-pythonic&#8221; and it is recommended not to be used unless you desperately need it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/8236380\/why-is-semicolon-allowed-in-this-python-snippet\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/8236380\/why-is-semicolon-allowed-in-this-python-snippet<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The common meaning of a semicolon(;) in various programming languages is to end or discontinue the current statement. In programming languages such as C, C++, and Java, it is necessary to use a semicolon to end a line of code. However, this is not the case with Python. A semicolon in Python signifies separation rather [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":12693,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12650","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\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=12650"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12650\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/12693"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=12650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=12650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=12650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}