The
print
statement takes a list of values and,
well, prints them. Speaking strictly, it does two things: it converts
the objects to strings and puts the characters of those strings on
standard output. Generally, standard output is
the console window where Python was started, although there are ways to
change this that are beyond the scope of this book.
Here's a quick summary of the more important features of
print
statement syntax. In short, the keyword,
print
, is followed by a comma-separated list of
expressions.
Note
This syntax summary isn't completely correct because it implies
that the list of expressions is
terminated
with a
comma. Rather than fuss around with complex syntax diagrams (that's
what the Python reference manual is for) we've show an approximation
that is close enough.
The ,
in a
print
statement is
used to
separate
the various expressions.
A ,
can also be used at the end of the
print
statement to change the formatting; this is
an odd-but-true feature that is unique to
print
statement syntax.
It's hard to capture all of this sublety in a single syntax
diagram.
One of the simplest kind of expressions is a quoted string. You
can use apostrophes ('
) or quotes ("
) to
surround strings. This gives you some flexibility in your strings. You
can put an apostrophe into a quoted string, and you can put quotes into
an apostrophe'd string without special escapes
that some other languages require. The full set of quoting rules and
alternatives, however, will have to wait for Chapter 12, Strings
.
For example, the following trivial program prints three strings
and two numbers.
print "Hi, Mom", "Isn't it lovely?", 'I said, "Hi".', 42, 91056
Multi-Line Output. Ordinarily, each
print
statement produces one
line of output. You can end the
print
statement
with a trailing
,
to combine the results of
multiple
print
statements into a single line. Here
are two examples.
print "335/113=",
print 335.0/113.0
print "Hi, Mom", "Isn't it lovely?",
print 'I said, "Hi".', 42, 91056
Since the first
print
statement ends with a
,
it does not produce a complete line of output. The
second
print
statement finishes the line of
output.
Redirecting Output. The
print
statement's output goes to the
operating system's standard output file. How do we send output to the
system's standard error file? This involves some more advanced
concepts, so we'll introduce it with a two-part recipe that we need to
look at in more depth. We'll revisit these topics in Part IV, “Components, Modules and Packages”.
First, you'll need access to the standard error object; you get
this via the following statement.
import sys
Second, there is an unusual piece of syntax called a "chevron
print" which can be used to redirect output to standard error.
print
>>
file
, [
expression
, ]
The file can be either sys.stdout
or
sys.stderr
. Here is an example of a small script
which produces messages on both stderr and stdout.
Example 4.1. mixedout.py
#!/usr/bin/env python
"""Mixed output in stdout and stderr."""
import sys
print >>sys.stderr, "This is an error message"
print "This is stdout"
print >>sys.stdout, "This is also stdout"
When you run this inside IDLE, you'll
notice that the stderr is colored red, where the stdout is colored
black. You'll also notice that the order of the output in
IDLE doesn't match the order in our program.
Most POSIX operating systems buffer stdout, but does not buffer stderr.
Consequently, stdout messages don't appear until the buffer is full, or
the program exits.