In Python programming language, by default print statement adds a newline character. So when we have multiple print statements the output from each of them is printed in multiple lines as you can see in the example below. Our goal is to print them in a single line or python print without newline and use some special parameters to the print function to achieve that.
Python print() function
To print the data, we use the print function. It is very easy to use. Let’s have a look at one example of the print function.
print("first line") print("second line") print"third line")
The output is:
first line seconde line third line
We have to pass the data that we wish to print in the print function. It is this easy to print data in python. But observe the output of the above python code. We used three print functions and each value is printed in a different line. There is no problem with it but sometimes we want that data should be printed on the same line.
However, In some cases, we may want to output multiple strings on the same line using separate
Python print without newline in Python3
In Python 3, print() is a function that prints output on different lines, every time you use the function. Python 3 provides the simplest solution, all you have to do is to provide one extra argument to the print function.
# use the argument "end" to specify the end of line string print("Good Morning!", end = '') print("Have a wonderful day!")
Good Morning!Have a wonderful day!
Explanation Using end Parameter
Here we are using the end parameter to use a specific character at the end of the output from each print statement. In the above example, we used “,” as a special character with the end parameter, which will appear at the end of the output of each print statement. The result will not be printed in multiple lines.
Must Read: Python Null
Python print without newline in Python2
Unlike Python 3, Python 2 does not have the ‘end’ function. For the python version 2.0 and above, we have to use a different approach. Instead of using the end keyword, We can simply separate the print function by a comma to print without a newline.
# no newlines but a space will be printed out print "Hello World!", print "This is Daniel from Python Pool"
Hello World! This is Daliel from Python Pool
As you can see, even though there were no newlines, we still got a space character between the two print statements.
Python print without newline using the inbuilt library
To print without a newline in python, there is another method. In this, you can use the powerful sys.stdout.write a function from the sys module.
The Benefits of using inbuilt-library are:
- The function will only print whatever you explicitly tell it to print.
- There are no terminating strings.
<pre class="wp-block-syntaxhighlighter-code">#Import the inbuilt sys library import sys #First Line of output sys.stdout.write("Hey! Welcome to <a href="https://www.pythonpool.com/" target="_blank" rel="noreferrer noopener">Python Pool</a>.") #Second Line of output sys.stdout.write("We have the best python tutorials")</pre>
<pre class="wp-block-syntaxhighlighter-code">Hey! Welcome to <a href="https://www.pythonpool.com/" target="_blank" rel="noreferrer noopener">Python Pool</a>.We have the best python tutorials</pre>
Note: The output of both the example above will be in one line without adding space in between the strings.
For Python print without newline In Python 3, you can use the named end argument in the print function and assign an empty string to this argument to prevent the terminating newline.
or you can just use the sys.stdout.write() function.
If you didn’t find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.Team Python Pool