The Print Function

If you have Python 2.6, you can start using with the print function. The print function is not part of Python 2.6, it's a future expansion. To ease the transition from older to newer language features, there is a __future__ module available. You can get this future expansion by using the following statement at the beginning of your script or working session in IDLE.

from __future__ import print_function

This will alert Python that you want to use the print function instead of the print statement. We'll look at the import statement in depth in Part IV, “Components, Modules and Packages”.

The print function has the following formal definition.

print (object,..., [,sep, ][,end, ][,file]) → number

Convert the objects to strings and write the strings to the given file.

If sep is not specified, it is a single space. If end is not specified, it is a single \n. If the file is not specified it is sys.stdout.

Examples:

from __future__ import print_function
import sys

print("335/113=",335.0/113.0)

print("Hi, Mom", "Isn't it lovely?", end='')
print('I said, "Hi".', 42, 91056)

print("Red Alert!", file-sys.stderr)

The print statement uses a trailing , to suppress the newline that defines the end of a line of output. To do this with the print function, use end=''.

To send output to standard error, use file=sys.stderr.