Online Book Reader

Home Category

Learning Python - Mark Lutz [178]

By Root 1340 0
the end-of-line character normally added at the end of the printed text (not to be confused with tuple syntax!). The >> syntax, normally used as a bitwise right-shift operation, is used here as well, to specify a target output stream other than the sys.stdout default.

Table 11-5. Python 2.6 print statement forms

Python 2.6 statement

Python 3.0 equivalent

Interpretation

print x, y

print(x, y)

Print objects’ textual forms to sys.stdout; add a space between the items and an end-of-line at the end

print x, y,

print(x, y, end='')

Same, but don’t add end-of-line at end of text

print >> afile, x, y

print(x, y, file=afile)

Send text to myfile.write, not to sys.stdout.write

The 2.6 print statement in action

Although the 2.6 print statement has more unique syntax than the 3.0 function, it’s similarly easy to use. Let’s turn to some basic examples again. By default, the 2.6 print statement adds a space between the items separated by commas and adds a line break at the end of the current output line:

C:\misc> c:\python26\python

>>>

>>> x = 'a'

>>> y = 'b'

>>> print x, y

a b

This formatting is just a default; you can choose to use it or not. To suppress the line break so you can add more text to the current line later, end your print statement with a comma, as shown in the second line of Table 11-5 (the following is two statements on one line, separated by a semicolon):

>>> print x, y,; print x, y

a b a b

To suppress the space between items, again, don’t print this way. Instead, build up an output string using the string concatenation and formatting tools covered in Chapter 7, and print the string all at once:

>>> print x + y

ab

>>> print '%s...%s' % (x, y)

a...b

As you can see, apart from their special syntax for usage modes, 2.6 print statements are roughly as simple to use as 3.0’s function. The next section uncovers the way that files are specified in 2.6 prints.

Print Stream Redirection

In both Python 3.0 and 2.6, printing sends text to the standard output stream by default. However, it’s often useful to send it elsewhere—to a text file, for example, to save results for later use or testing purposes. Although such redirection can be accomplished in system shells outside Python itself, it turns out to be just as easy to redirect a script’s streams from within the script.

The Python “hello world” program

Let’s start off with the usual (and largely pointless) language benchmark—the “hello world” program. To print a “hello world” message in Python, simply print the string per your version’s print operation:

>>> print('hello world') # Print a string object in 3.0

hello world

>>> print 'hello world' # Print a string object in 2.6

hello world

Because expression results are echoed on the interactive command line, you often don’t even need to use a print statement there—simply type the expressions you’d like to have printed, and their results are echoed back:

>>> 'hello world' # Interactive echoes

'hello world'

This code isn’t exactly an earth-shattering piece of software mastery, but it serves to illustrate printing behavior. Really, the print operation is just an ergonomic feature of Python—it provides a simple interface to the sys.stdout object, with a bit of default formatting. In fact, if you enjoy working harder than you must, you can also code print operations this way:

>>> import sys # Printing the hard way

>>> sys.stdout.write('hello world\n')

hello world

This code explicitly calls the write method of sys.stdout—an attribute preset when Python starts up to an open file object connected to the output stream. The print operation hides most of those details, providing a simple tool for simple printing tasks.

Manual stream redirection

So, why did I just show you the hard way to print? The sys.stdout print equivalent turns out to be the basis of a common technique in Python. In general, print and sys.stdout are directly related as follows. This statement:

print(X, Y) # Or, in 2.6: print X, Y

is equivalent to the

Return Main Page Previous Page Next Page

®Online Book Reader