Introduction
The print()
function in Python appends a newline to the output when displayed on the tty (teletypewriter A.K.A the terminal). When you don't want your message displayed with newlines or with spaces, how can you change the behavior of print()
?
This can easily be achieved by altering the default values of the sep
and end
parameters of the print()
function.
Printing without a Newline
Until Python version 2.x, print
was a reserved keyword that acts as a special statement. Since Python version 3.x, the print
command has evolved into a function.
This version of print()
is capable of taking the following arguments:
The values (value1
, value2
) mentioned above can be any string or any of the data types like list, float, string, etc. The other arguments include a separator (sep
) used to divide the values given as arguments whereas the argument end
is the \n
newline character by default. This is the reason why whenever the print()
function is called, the cursor slides to the next line.
In Python 3.x, the most straightforward way to print without a newline is to set the end
argument as an empty string i.e. ''
. For example, try executing the following snippet in your Python interpreter:
print("I am a sentence", "I am also a sentence")
The interpreter would output the following:
I am a sentence I am also a sentence
>>>
We are printing two strings, so Python will use the value of sep
, a blank space by default, to print them together. Python also adds a newline character at the end, so the interpreter prompt goes to the end line.
Now modify the previous statement to look like this:
print("I am a sentence", "I am also a sentence", sep="; ", end="")
Upon executing it in the interpreter, you will get an output resembling:
I am a sentence; I am also a sentence>>>
Two things happened here - the separator between the two strings now also includes a semicolon. The interpreter prompt also appears on the same line because we removed the automatically appended newline character.
Printing Without a Newline in Python 2.X
For earlier versions of Python - less than 3 but greater than 2.6 - you can import print_function
from the __future__
module. This will override the existing print
keyword with the print()
function as shown below:
from __future__ import print_function
print("I am a sentence", "I am also a sentence", sep="; ", end="")
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
This will also yield:
I am a sentence; I am also a sentence>>>
This is how you can use Python version 3's print()
function in Python 2.x.
Using stdout.write()
The sys
module has built-in functions to write directly to the file or the tty. This function is available for Python 2.x and 3.x versions. We can use the write()
method of the sys
module's stdout
object to print on the console like this:
import sys
sys.stdout.write("I am a line")
Let's execute this and take a look at the output:
I am a line>>>
Although this gives the output of what we are trying to achieve, there are quite a few differences between the write()
function and the print()
function. The print()
function can print multiple values at a time, can accept non-string values, and is friendlier to developers.
Conclusion
In this article, we have explored the different ways by which values can be printed without a newline character/carriage return. This strategy can come quite handy while printing the elements in the outputs of algorithms such as a binary tree or printing the contents of a list next to each other.