Topic focus: The print
function
One of the most basic functions in Python is the print
function,
which is used to print values and variables to the terminal.
In this focus reading we'll cover the most essential aspects
of this function.
Basic use of print
The print
function is typically used to print a Python value
or the value of a Python variable to the terminal.
Here are some examples using the Python interactive interpreter:
>>> print(10)
10
>>> print("hello, world!")
hello, world!
>>> x = 10
>>> print(x)
10
>>> s = "hello, world!")
>>> print(s)
hello, world!
What print does is as follows:
- It takes its argument and converts it into a string,
- it prints the string,
- then it prints a newline character.
The print
function can be used on any Python value,
no matter how simple or complex.
That's because Python is capable of converting any Python value
to a string.
print
return value
All Python functions are required to return a value.
Since print
doesn't really have any value that it needs to return,
it simply returns the None
value,
which is a value with no significance.
You can see None
values in some cases:
>>> 10
10
>>> None <-- NOTHING GETS PRINTED AFTER THIS LINE
>>> print(None)
None
>>> print(print(10))
10
None
When you are writing code in the interactive Python interpreter,
it will evaluate whatever you type in and (usually) print the result
automatically.
(This does not happen when you run Python code from a file!)
The exception to this is the None
value.
As the previous example showed,
when you enter None
directly,
nothing gets printed, because a None
value is not interesting.
However, if you ask Python to print a None
value
using the print
function,
it will go ahead and print the string None
.
If you type print(print(10))
, this happens:
-
Python will evaluate the argument to the outermost
print
function call (print(print(10))
), which is another print function call (print(10)
). This causes the number10
to be printed. -
The inner
print
function call (print(10)
) returns aNone
value, so the outermostprint
function call will print that value. This causes the stringNone
to be printed.
The order that this happens is significant. Python always evaluates all the arguments to a function before calling the function. If there is more than one argument, It evaluates them from left to right, as we can see here:
Printing no or multiple items
In the last example, we saw that we can print multiple values
in a single call to the print
function.
How does that work?
print
is an unusual function!
Most Python functions take a specific number of arguments.
Giving fewer or more arguments than what the function expects
will result in an error.
However, the print
function is different.
It can take any number of arguments, including zero!
The default behavior of print
when given multiple arguments
is to print them all on the same line,
separated by a single space character.
When you give print
no arguments (print()
),
this just prints a newline character.
Changing the separator for multiple items
If you want to print multiple items in a single print
call,
but want them to be separated by something other than a space,
you can do it by using the sep
keyword argument:
>>> print(1, 2, 3)
1 2 3
>>> print(1, 2, 3, sep=", ")
1, 2, 3
>>> print(1, 2, 3, sep="\n")
1
2
3
>>> print(1, 2, 3, sep="")
123
Keyword arguments
Keyword arguments are just a way of giving a name to an argument
to make function calls easier to understand,
and also to make the argument optional.
They are just a name (like sep
) followed by the equal sign (=
)
and the value of the argument.
We'll discuss keyword arguments in a separate reading.
Suppressing the printing of the final newline character
Sometimes you may not want the print
function to print a newline
after printing the value.
In this case, you can use the end
keyword argument
to suppress printing the newline:
Here you see that after printing 42
, no newline is printed,
so the Python interpreter prints the prompt
immediately after the 42
.
This isn't useful interactively,
but can be useful when using print
in a file of Python code.
You can also use different arguments for the end
argument:
This call prints two newlines after printing the number.
For more information
Core lecture 4, starting
here,
has more information on print
and string formatting.