Section 2.1. Program Output, the print Statement, and Hello World


2.1. Program Output, the print Statement, and "Hello World!"

In some languages, such as C, displaying to the screen is accomplished with a function, e.g., printf(), while with Python and most interpreted and scripting languages, it is a statement. Many shell script languages use an echo command for program output.

Core Note: Dumping variable contents in interactive interpreter

Usually when you want to see the contents of a variable, you use the print statement in your code. However, from within the interactive interpreter, you can use the print statement to give you the string representation of a variable, or just dump the variable rawthis is accomplished by simply giving the name of the variable.

In the following example, we assign a string variable, then use print to display its contents. Following that, we issue just the variable name.

>>> myString = 'Hello World!' >>> print myString Hello World! >>> myString 'Hello World!'


Notice how just giving only the name reveals quotation marks around the string. The reason for this is to allow objects other than strings to be displayed in the same manner as this stringbeing able to display a printable string representation of any object, not just strings. The quotes are there to indicate that the object whose value you just dumped to the display is a string. Once you become more familiar with Python, you will recognize that str() is used for print statements, while repr() is what the interactive interpreter calls to display your objects.

The underscore (_) also has special meaning in the interactive interpreter: the last evaluated expression. So after the code above has executed, _ will contain the string:

 >>> _  Hello World!



Python's print statement, paired with the string format operator ( % ), supports string substitution, much like the printf() function in C:

  >>> print "%s is number %d!" % ("Python", 1)   Python is number 1!


%s means to substitute a string while %d indicates an integer should be substituted. Another popular one is %f for floating point numbers. We will see more examples throughout this chapter. Python is fairly flexible, though, so you could pass in a number to %s without suffering any consequences with more rigid languages. See Section 6.4.1 for more information on the string format operator.

The print statement also allows its output directed to a file. This feature was added way back in Python 2.0. The >> symbols are used to redirect the output, as in this example with standard error:

import sys print >> sys.stderr, 'Fatal error: invalid input!'


Here is the same example with a logfile:

  logfile = open('/tmp/mylog.txt', 'a')   print >> logfile, 'Fatal error: invalid input!'   logfile.close()




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net