{"id":9249,"date":"2020-04-21T10:00:20","date_gmt":"2020-04-21T04:30:20","guid":{"rendered":"https:\/\/java2blog.com\/?p=9249"},"modified":"2020-12-23T22:40:36","modified_gmt":"2020-12-23T17:10:36","slug":"python-print-without-newline","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-print-without-newline\/","title":{"rendered":"Python Print without Newline"},"content":{"rendered":"<p>In this post, we will see how to print without Newline in Python.<\/p>\n<p>In python, when you use two print statements consecutively, it will print in different line.<\/p>\n<pre class=\"lang:python decode:1 \" >\nprint('Hello world')\nprint('from Java2blog')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nHello world<br \/>\nfrom Java2blog\n<\/div>\n<p>As expected, we got <code>Hello world<\/code> and <code>from Java2blog<\/code><\/p>\n<p>If you want to print these two print statements in the same line, you can do it in different ways.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Using_print_statement\">Using print statement<\/a><ul><li><a href=\"#Print_with_space\">Print with space<\/a><\/li><li><a href=\"#Print_with_comma\">Print with comma(,)<\/a><\/li><\/ul><\/li><li><a href=\"#Using_sys_library\">Using sys library<\/a><\/li><\/ul><\/div>\n\n<blockquote><p>\n<span style=\"font-size: 1.2em\">&#x1f4a1; <strong>Did you know?<\/strong><\/style>\n<p>If you are using python 2, then you can simply put comma(<code>,<\/code>) to print statements in same line.<\/p>\n<pre class=\"lang:python decode:1 \" >\n#print without new line but with space\nprint('Hello world'),\nprint('from Java2blog')\n<\/pre>\n<\/blockquote>\n<h2><span id=\"Using_print_statement\">Using print statement<\/span><\/h2>\n<p>You can use <code>end='$delimiter'<\/code> with print statements and it will print string with delimiter.<\/p>\n<h3><span id=\"Print_with_space\">Print with space<\/span><\/h3>\n<p>You can use <code>end=' '<\/code> for printing statements separated by space.<\/p>\n<pre class=\"lang:python decode:1 \" >\nprint('Hello world',end=' ')\nprint('from Java2blog')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-blue\">\nHello world from Java2blog\n<\/div>\n<h3><span id=\"Print_with_comma\">Print with comma(,)<\/span><\/h3>\n<p>You can use <code>end=','<\/code> for printing statements separated by comma.<\/p>\n<pre class=\"lang:python decode:1 \" >\nprint('Hello world',end=',')\nprint('from Java2blog')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nHello world,from Java2blog\n<\/div>\n<h2><span id=\"Using_sys_library\">Using sys library<\/span><\/h2>\n<p>You can also import <code>sys<\/code> library and use <code>sys.stdout.write()<\/code> to print on same line.<\/p>\n<pre class=\"lang:python decode:1 \" >\nimport sys\nsys.stdout.write('Hello world')\nsys.stdout.write(' from Java2blog')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nHello worldfrom Java2blog\n<\/div>\n<p>Printing without newline is very useful while printing using a loop. If you don&#8217;t want to print each element in an individual line, you can use <code>end=' '<\/code> with print statement to print on the same line.<\/p>\n<pre class=\"lang:python decode:1 \" >\ncountryList =[\"India\",\"China\",\"Russia\",\"France\"]\nfor country in countryList:\n    print(country,end=' ')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-grey\">\nIndia China Russia France\n<\/div>\n<p>As you can see, all the elements are printed on same line separated by space(<code>' '<\/code>)<\/p>\n<p>That&#8217;s all about Python Print without Newline.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing print statementPrint with spacePrint with comma(,)Using sys library In this post, we will see how to print without Newline in Python. In python, when you use two print statements consecutively, it will print in different line. print(&#8216;Hello world&#8217;) print(&#8216;from Java2blog&#8217;) Output: Hello world from Java2blog As expected, we got Hello world and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,186],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9249"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=9249"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9249\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=9249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=9249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=9249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}