Python print(): Display Output
Another built-in function
print() serves as an output statement in Python. It echoes the value of any Python expression on the Python shell.
Multiple values can be displayed by the single
print() function separated by comma.
The following example displays values of
age variables using the single
>>> name="Ram" >>> print(name) # display single variable Ram >>> age=21 >>> print(name, age)# display multiple variables Ram 21 >>> print("Name:", name, ", Age:",age) # display formatted output Name: Ram, Age: 21
By default, a single space
' ' acts as a separator between values. However, any other character can be used by providing a
In the following example,
, is used as a separator character.
>>> name="Ram" >>> age=21 >>> print(name, age, sep=",") Ram,21
The output of the
print() function always ends by the NEWLINE character.
print() function has another optional parameter
end, whose default value is
\n, which can be substituted by any other character such as a single space (' ') to display the output of the subsequent
print() statement in the same line, as shown below.
>>> name="Bill" >>> age=21 >>> print(name, end=" "); print(age) Bill 21
Note that the output is displayed in a single line even if there are two
It is possible to format the output using C style format specifier symbols such as
%d, %f, %s, etc. Learn about it in the Python String.