4.8 The print Statement
A print statement is denoted by the keyword print
followed by zero or more expressions separated by commas.
print is a handy, simple way to output values in
text form. print outputs each expression
x as a string that's just
like the result of calling
str(x)
(covered in Chapter 8). print
implicitly outputs a space between expressions, and it also
implicitly outputs \n after the last expression,
unless the last expression is followed by a trailing comma
(,). Here are some examples of
print statements:
letter = 'c'
print "give me a", letter, "..." # prints: give me a c ...
answer = 42
print "the answer is:", answer # prints: the answer is: 42
The destination of print's output
is the file or file-like object that is the value of the
stdout attribute of the sys
module (covered in Chapter 8). You can control
output format more precisely by performing string formatting
yourself, with the % operator or other string
manipulation techniques, as covered in Chapter 9.
You can also use the write or
writelines methods of file objects, as covered in
Chapter 10. However, print is
very simple to use, and simplicity is an important advantage in the
common case where all you need are the simple output strategies that
print supplies.
|