{"id":527,"date":"2018-03-02T05:54:37","date_gmt":"2018-03-02T05:54:37","guid":{"rendered":"http:\/\/www.afternerd.com\/blog\/?p=527"},"modified":"2019-01-13T03:33:02","modified_gmt":"2019-01-13T03:33:02","slug":"how-to-print-without-a-newline-in-python","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/how-to-print-without-a-newline-in-python\/","title":{"rendered":"Python: How to Print Without Newline? (The Idiomatic Way)"},"content":{"rendered":"<p>Jump straight to the solution:<\/p>\n<p><a href=\"#python3\">Printing without a newline in Python3<\/a><\/p>\n<p><a href=\"#python2\">Printing without a newline in Python2<\/a><\/p>\n<h2>The Problem<\/h2>\n<p>Python is one of the easiest programming languages to learn.<\/p>\n<p>One of the first programs that you write when you start learning any new programming language is a\u00a0<a href=\"https:\/\/www.afternerd.com\/blog\/hello-world\" target=\"_blank\" rel=\"noopener\"><em>hello world<\/em><\/a> program.<\/p>\n<p>A\u00a0<em>hello world<\/em> program in python looks like this<\/p>\n<pre class=\"prettyprint\"><code># hello world in python\r\nprint(\"Hello World!\")\r\n<\/code><\/pre>\n<p>It is that easy!<\/p>\n<p>Just one line and boom you have your hello world program.<\/p>\n<p>In Python 3, <a href=\"https:\/\/docs.python.org\/3\/whatsnew\/3.0.html#print-is-a-function\" target=\"_blank\" rel=\"noopener\">print() is a function<\/a> that prints out things onto the screen <a href=\"https:\/\/snarky.ca\/why-print-became-a-function-in-python-3\/\" target=\"_blank\" rel=\"noopener\">(print was a statement in Python 2)<\/a>.<\/p>\n<p>As you can see, it is a very simple function.<\/p>\n<p>Yet there is one thing that is really annoying about this function.<\/p>\n<p>It automatically prints a newline &#8216;\\n&#8217; at the end of the line!<\/p>\n<p>Let&#8217;s take a look at this example<\/p>\n<pre class=\"prettyprint\"><code>print(\"Hello World!\")\r\nprint(\"My name is Karim\")\r\n# output:\r\n# Hello World!\r\n# My name is Karim<\/code><\/pre>\n<p>As you can notice, the two strings are not printed one after the other on the same line but on separate lines instead.<\/p>\n<p>Even though this might be what you actually want, but that&#8217;s not always the case.<\/p>\n<p>if you&#8217;re coming from another language, you might be more comfortable with explicitly mentioning whether a newline should be printed out or not.<\/p>\n<p>For example in Java, you have to explicitly indicate your desire to print a newline by either using the println function or typing the newline character (\\n) inside your print function:<\/p>\n<pre class=\"prettyprint\"><code>\/\/ option 1\r\nSystem.out.println(\"Hello World!\")\r\n\/\/ option 2\r\nSystem.out.print(\"Hello World!\\n\")<\/code><\/pre>\n<p>So what should we do if we want no newline characters in python?<\/p>\n<p>Let&#8217;s jump right into the solution!<\/p>\n<h2>The Solution<\/h2>\n<h3 id=\"python3\">Printing with no newlines in Python 3<\/h3>\n<p>Python 3 provides the simplest solution, all you have to do is to provide one extra argument to the print function.<\/p>\n<pre class=\"prettyprint\"><code># use the named argument \"end\" to explicitly specify the end of line string\r\nprint(\"Hello World!\", end = '')\r\nprint(\"My name is Karim\")\r\n# output:\r\n# Hello World!My name is Karim\r\n<\/code><\/pre>\n<p>You can use the optional named argument <strong>end<\/strong> to explicitly mention the string that should be appended at the end of the line.<\/p>\n<p>Whatever you provide as the\u00a0<strong>end\u00a0<\/strong>argument is going to be the terminating string.<\/p>\n<p>So if you provide an empty string, then no newline characters, and no spaces will be appended to your input.<\/p>\n<h3 id=\"python2\">Printing with no newlines in Python 2<\/h3>\n<p>In python 2, the easiest way to avoid the terminating newline is to use a comma at the end of your print statement<\/p>\n<pre class=\"prettyprint\"><code># no newlines but a space will be printed out\r\nprint \"Hello World!\",\r\nprint \"My name is Karim\"\r\n# output\r\n# Hello World! My name is Karim\r\n<\/code><\/pre>\n<p>As you can see, even though there was no newlines, we still got a space character between the two print statements.<\/p>\n<p>If you actually needed a space, then this is the simplest and most straightforward way to go.<\/p>\n<p>But what if you want to print without a space or newline?<\/p>\n<p>In this you can, you should use the all-powerful <strong>sys.stdout.write\u00a0<\/strong>function from the <a href=\"https:\/\/docs.python.org\/3\/library\/sys.html\" target=\"_blank\" rel=\"noopener\">sys<\/a> module.<\/p>\n<p>This function will only print whatever you explicitly tell it to print.<\/p>\n<p>There are no terminating strings.<\/p>\n<p>There is no magic!<\/p>\n<p>Let&#8217;s take an example<\/p>\n<pre class=\"prettyprint\"><code>import sys\r\n\r\nsys.stdout.write(\"Hello World!\")\r\nsys.stdout.write(\"My name is Karim\")\r\n# output\r\n# Hello World!My name is Karim\r\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In Python 3, you can use the named\u00a0<strong>end\u00a0<\/strong>argument in the print function and assign an empty string to this argument to prevent the terminating newline.<\/p>\n<p>In Python 2, you can either use a comma after your print statement if you don&#8217;t mind the space, or you can just use the\u00a0<strong>sys.stdout.write()<\/strong> function<\/p>\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":530,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[165,219,152,160,146,147,220,164,159,154,157,163,153,570,53,158,149,150,218,155],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/527"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":22,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":1271,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions\/1271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/530"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=527"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}