The simple truth is that print
and return
have nothing to do with each other. print
is used to display things in the terminal (for command-line programs).1 return
is used to get a result back when you call a function, so that you can use it in the next step of the program's logic.
Many beginners are confused when they try out code at Python's interpreter prompt2, like
>>> def example():
... return 1
...
>>> example()
1
The value was displayed; doesn't this mean that return
displays things? No. If you try the same code in a .py
file, you can see for yourself that running the script doesn't cause the 1
to display.
This shouldn't actually be confusing, because it works the same way as any other expression:
>>> 1 + 1
2
This displays at the interactive prompt, but not if we make a script that just says 1 + 1
and try running it.
Again: if you need something to display as part of your script, print
it. If you need to use it in the next step of the calculation, return
it.
The secret is that the interactive prompt is causing the result to be displayed, not the code. It's a separate step that the prompt does for you, so that you can see how the code works a step at a time, for testing purposes.
Now, let's see what happens with print
:
>>> def example():
... return 'test'
...
>>> print(example())
test
The result will display, whether we have this in an interactive prompt or in a script. print
is explicitly used to display the value - and as we can see, it displays differently. The interactive prompt uses what is called the repr
of the value that was returned from example
, while print
uses the str
of the value.
In practical terms: print
shows us what the value looks like, in text form (for a string, that just means the contents of the string as-is). The interactive prompt shows us what the value is - typically, by writing something that looks like the source code we would use to create it.3
But wait - print
is a function, right? (In 3.x, anyway). So it returned a value, right? Isn't the interpreter prompt supposed to display that in its separate step? What happened?
There is one more trick: print
returns the special value None
, which the interpreter prompt will ignore. We can test this by using some expressions that evaluate to None:
>>> None
>>> [None][0]
>>> def example():
... pass # see footnote 4
...
>>> example()
>>>
In each case, there is no separate line at all for output, not even a blank line - the interpreter prompt just goes back to the prompt.
1 It can also be used to write into files, although this is a less common idea and normally it will be clearer to use the .write
method.
2 This is sometimes called the REPL, which stands for "read-eval-print loop".
3 This isn't always practical, or even possible - especially once we start defining our own classes. The firm rule is that repr
will lean on the .__repr__
method of the object to do the dirty work; similarly, str
leans on .__str__
.
4 Functions in Python implicitly return None
if they don't explicitly return a value.