One of the first things that anyone learns in Python is (of course) how to print the string, “Hello, world.” As you would expect, the code is straightforward and simple:
print('Hello, world')
And indeed, Python’s “print” function is so easy and straightforward to use that we barely give it any thought. We assume that people know how to use it — and for the most part, for most of the things they want to do, that’s true.
But lurking beneath the surface of the “print” function is a lot of functionality, as well as some history (and even a bit of pain). Understanding how to use “print” can cut down on the code you write, and generally make it easier for you to work with.
The basics
The basics are simple: “print” is a function, which means that if you want to invoke it, you need to use parentheses:
>>> print('hello') hello
You can pass any type of data to “print”. Strings are most common, but you can also ints, floats, lists, tuples, dicts, sets, or any other object. For example:
>>> print(5) 5
or
>>> print([10, 20, 30]) [10, 20, 30]
And of course, it doesn’t matter whether the thing you’re trying to print is passed as a literal object, or referenced by a variable:
>>> d = {'a':1, 'b':2, 'c':3}>>> print(d) {'a':1, 'b':2, 'c':3}
You can also put an expression inside of the parentheses; the value of the expression will be passed to “print”:
>>> print(3+5) 8 >>> print([10, 20] + [30, 40]) [10, 20, 30, 40]
Every object in Python knows how to display itself as a string, which means that you can pass it directly to “print”. There isn’t any need to turn things into strings before handing them to “print”:
print(str([10, 20, 30]) # unnecessary use of "str" [10, 20, 30]
After “print” displays its output, it adds a newline. For example:
>>> print('abc') >>> print('def') >>> print('ghi') abc def ghi
You can pass as many arguments as you want to “print”, separated by commas. Each will be printed, in order, with a space between them:
>>> print('abcd', 'efgh', [10, 20, 30], 99, 'ijkl') abcd efgh [10, 20, 30] 99 ijkl
We’ll see, below, how we can change these two default behaviors.
Inputs and outputs
If “print” is a function, then it must have a return value. Let’s take a look:
>>> x = print('abcd') >>> type(x) NoneType
In other words: “print” returns None, no matter what you print. After all, you’re not printing in order to get a return value, but rather for the side effect.
What about arguments to “print”? Well, we’ve already seen that we can pass any number of arguments, each of which will be printed. But there are some optional parameters that we can pass, as well.
The two most relevant ones allow us to customize the behavior we saw before, changing what string appears between printed items and what is placed at the end of the output.
The “sep” parameter, for example, defaults to ‘ ‘ (a space character), and is placed between printed items. We can set this to any string, including a multi-character string:
>>> print('a', 'b', 'c', sep='*') a*b*c >>> print('abc', 'def', 'ghi', sep='***') abc***def***ghi >>> print([10, 20, 30], [40, 50, 60], [70, 80, 90], sep='***') [10, 20, 30]***[40, 50, 60]***[70, 80, 90]
Notice that “sep” is placed between the arguments to “print”, not between the elements of each argument. Thus in this third example, the ‘***’ goes between the lists, rather than between the integer elements of the lists.
If you want the arguments to be printed alongside one another, you can set “sep” to be an empty string:
>>> print('abc', 'def', 'ghi', sep='') abcdefghi
Similarly, the “end” parameter defaults to ‘\n’ (newline), but can contain any string. It determines what’s printed after “print” is done.
For example, if you want to have some extra lines after you print something, just change “end” so that it has a few newlines:
>>> def foo(): print('abc', end='\n\n\n') print('def', end='\n\n\n') >>> foo() abc def
If, by contrast, you don’t want “print” to add a newline at the end of what you print, you can set “end” to be an empty string:
>>> def foo(): print('abc', end='') print('def', end='') >>> foo() abcdef>>>
Notice how in the Python interactive shell, using the empty string to print something means that the next ‘>>>’ prompt comes after what you printed. After all, you didn’t ask for there to be a newline after what you wrote, and Python complied with your request.
Of course, you can pass values for “end” that don’t involve newlines at all. For example, let’s say that you want to output multiple fields to the screen, with each field printed in a separate line:
>>> def foo(): print('abc', end=':') print('def', end=':') print('ghi') >>> foo() abc:def:ghi
Printing to files
By default, “print” sends its data to standard output, known in Python as “sys.stdout”. While the “sys” module is automatically loaded along with Python, its name isn’t available unless you explicitly “import sys”.
The “print” function lets you specify, with the “file” parameter, another file-like object (i.e., one that adheres to the appropriate protocol) to which you want to write. The object must be writable, but other than that, you can use any object.
For example:
>>> f = open('myfile.txt', 'w') >>> print('hello') hello >>> print('hello???', file=f) >>> print('hello again') hello again >>> f.close() >>> print(open('myfile.txt').read()) hello???
In this case, the output was written to a file. But we could also have written to a StringIO object, for example, which acts like a file but isn’t one.
Note that if I hadn’t closed “f” in the above example, the output wouldn’t have arrived in the file. That’s because Python buffers all output by default; whenever you write to a file, the data is only actually written when the buffer fills up (and is flushed), when you invoke the “flush” method explicitly, or when you close the file, and thus flush implicitly. Using the “with” construct with a file object closes it, and thus flushes the buffers as well.
There is another way to flush the output buffer, however: We can pass a True value to the “flush” parameter in “print”. In such a case, the output is immediately flushed to disk, and thus written. This might sound great, but remember that the point of buffering is to lessen the load on the disk and on the computer’s I/O system. So flush when you need, but don’t do it all of the time — unless you’re paid by the hour, and it’s in your interest to have things work more slowly.
Here’s an example of printing with and without flush:
>>> f = open('myfile.txt', 'w') >>> print('abc', file=f) >>> print('def', file=f) >>> print(open('myfile.txt').read()) # no flush, and thus empty file >>> print('ghi', file=f, flush=True) >>> print(open('myfile.txt').read()) # all data has been flushed to disk abc def ghi
You might have noticed a small inconsistency here: “print” writes to files, by default “sys.stdout”. And if we don’t flush or close the file, the output is buffered. So, why don’t we have to flush (or close, not that this is a good idea) when we print to the screen?
The answer is that “sys.stdout” is treated specially by Python. As the Python docs say, it is “line buffered,” meaning that every time we send a newline character (‘\n’), the output is flushed. So long as you are printing things to “sys.stdout” that end with a newline — and why wouldn’t you be doing that? — you won’t notice the buffering.
Remember Python 2?
As I write this, in January 2019, there are fewer than 12 months remaining before Python 2 is no longer supported or maintained. This doesn’t change the fact that many of my clients are still using Python 2 (because rewriting their large code base isn’t worthwhile or feasible). If you’re still using Python 2, you should really be trying to move to Python 3.
And indeed, one of the things that strikes people moving from Python 2 to 3 would be the differences in “print”.
First and foremost, “print” in Python 2 is a statement, not an expression. This means that the parentheses in 2 are optional, while they’re mandatory in 3 — one of the first things that people learn when they move from 2 to 3.
This also means that “print” in Python 2 cannot be passed to other functions. In Python 3, you can.
Python 2’s “print” statement didn’t have the parameters (or defaults) that we have at our disposal. You wanted to print to a file other than “sys.stdout”? Assign it to “sys.stdout” to use “print” — or just write to the file with the “write” method for files. You wanted “print” not to descend a line after printing? Put a comma at the end of the line. (Yes, really; this is ugly, but it works.)
What if you’re working in Python 2, and want to get a taste of Python 3’s print function? You can add this line to your code:
from __future__ import print_function
Once you have done so, Python 3’s “print” function will be in place.
Now I know that Python 3 is no longer in the future; indeed, you could say that Python 2 is in the past. But for many people who want to transition or learn how to do it, this is a good method. But watch out: If you have calls to “print” without parentheses, or are commas to avoid descending a line, then you’ll need to do more than just this import. You will need to go through your code, and make sure that it works in this way. So while that might seem like a wise way to to, it’s only the first step of a much larger transition from 2 to 3 that you’ll need to make.
Enjoyed this article? Join more than 11,000 other developers who receive my free, weekly “Better developers” newsletter. Every Monday, you’ll get an article like this one about software development and Python:
FYI Formatting seems to be a bit off in the code examples.
Grr, thanks for pointing that out; I’ll double (triple?) check that and fix.