{"id":2915,"date":"2020-02-01T06:52:13","date_gmt":"2020-02-01T06:52:13","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2915"},"modified":"2023-02-16T19:57:17","modified_gmt":"2023-02-16T19:57:17","slug":"python-print-without-newline","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-print-without-newline","title":{"rendered":"Python Print Without Newline"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are different ways through which we can print to the console without a newline. Let&#8217;s quickly go through some of these methods.<\/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 print()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can use the <code>print()<\/code> function to achieve this, by setting the <code>end<\/code> (ending character) keyword argument appropriately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, this is a newline character (<code>\\n<\/code>). So, we must change this to avoid printing a newline at the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are many options for this choice. We could use a space to print space-separated strings.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello&quot;\nb = &quot;from AskPython&quot;\n\nprint(a, end=&#039; &#039;)\nprint(b)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This would print strings <code>a<\/code> and <code>b<\/code>, separated by a single space, instead of a newline.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHello from AskPython\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We could also print them consecutively, without any gap, by using an empty string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &quot;Hello&quot;\nb = &quot;from AskPython&quot;\n\nprint(a, end=&#039;&#039;)\nprint(b)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHellofrom AskPython\n<\/pre><\/div>\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\">2. Printing list elements without a Newline<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, when iterating through a list, we may need to print all its elements on the same line. To do that, we can again use the same logic as before, using the <code>end<\/code> keyword argument.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;&quot;Hello&quot;, &quot;how&quot;, &quot;are&quot;, &quot;you?&quot;]\n\nfor i in a:\n    print(i, end=&quot; &quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHello how are you?\n<\/pre><\/div>\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\">3. Using the sys module<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can also use the <code>sys<\/code> <a href=\"https:\/\/www.askpython.com\/python-modules\/python-modules\" class=\"rank-math-link\">module<\/a> to print without a newline.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">More specifically, the <code>sys.stdout.write()<\/code> function enables us to write to the console without a newline.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys\nsys.stdout.write(&quot;Hello from AskPython.&quot;)\nsys.stdout.write(&quot;This is printed on the same line too!&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHello from AskPython.This is printed on the same line too!\n<\/pre><\/div>\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\">4. Creating our own C style printf() function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can also create our custom <code>printf()<\/code> function in Python! Yes, this is possible using the module <code>functools<\/code>, which allows us to define new functions from existing ones via <code>functools.partial()<\/code>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s use the same logic on the <code>end<\/code> keyword argument on <code>print()<\/code> and use it to create our <code>printf()<\/code> function!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport functools\n\n# Create our printf function using\n# print() invoked using the end=&quot;&quot;\n# keyword argument\nprintf = functools.partial(print, end=&quot;&quot;)\n\n# Notice the semicolon too! This is very familiar for a\n# lot of you!\nprintf(&quot;Hello!&quot;);\nprintf(&quot;This is also on the same line!&quot;);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHello!This is also on the same line!\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We can also combine the semicolon to this (Python compiler will not complain) to bring back our C <code>printf()<\/code> function as it was! <\/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\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JournalDev Article on printing without a newline<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/493386\/how-to-print-without-a-newline-or-space\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">StackOverflow Question<\/a> on the same topic<\/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>There are different ways through which we can print to the console without a newline. Let&#8217;s quickly go through some of these methods. 1. Using print() We can use the print() function to achieve this, by setting the end (ending character) keyword argument appropriately. By default, this is a newline character (\\n). So, we must [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2915","post","type-post","status-publish","format-standard","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2915","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=2915"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2915\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}