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
statement syntax. In short, the keyword,
print, is followed by a comma-separated list of
This syntax summary isn't completely correct because it implies
that the list of expressions is
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.
, in a
the various expressions.
, can also be used at the end of the
statement to change the formatting; this is
an odd-but-true feature that is unique to
It's hard to capture all of this sublety in a single syntax
One of the simplest kind of expressions is a quoted string. You
can use apostrophes (
') or quotes (
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
statement produces one
line of output. You can end the
with a trailing
to combine the results of
statements into a single line. Here
are two examples.
print "Hi, Mom", "Isn't it lovely?",
print 'I said, "Hi".', 42, 91056
Since the first
statement ends with a
it does not produce a complete line of output. The
statement finishes the line of
Redirecting Output. The
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.
Second, there is an unusual piece of syntax called a "chevron
print" which can be used to redirect output to standard error.
The file can be either
sys.stderr. Here is an example of a small script
which produces messages on both stderr and stdout.
Example 4.1. mixedout.py
"""Mixed output in stdout and stderr."""
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.