See All Titles |
![]() ![]() Input and OutputPython, as any other language, provides means to get input from the user and also to display information to him. Let's see how we can handle it. >>> x = input ("type anything: ") >>> print "You have typed ", x Note that the input prompt can be anything, even an empty one. If the user types 5, x is properly treated as a number. To make x become a string, the user must explicitly type the quotes. To avoid this problem, you can use the raw_input function: >>> x = raw_input ("type anything: ") >>> print "You have typed ", x Now, it doesn't matter whether the user types the quotes. Note that the print command requires objects to be separated by commas: >>> print "parrot", "sketch" parrot sketch Displaying InformationLet's delve a little bit deeper into this topic. Python has three standard file objects, which are available from the sys module. The interpreter uses them to provide input and output facilities. (Refer to Chapter 3, "Python Libraries," for details and examples—the sys module.) They are known as sys.stdin, sys.stdout, sys.stderr print statements are mapped to the sys.stdout. Hence, they send the textual representation of objects to the standard output stream: >>>import sys >>>sys.stdout.write("Nudge-nudge\n") Nudge-nudge Did you know that it is possible to re-map the standard output device? Yes, that is possible. You can run the following code to write to a file: >>> sys.stdout = open("outputtest.txt", "w") >>> print "hello" >>> sys.stdout.close >>> sys.stdout = sys.__stdout__ >>> sys.exit() Note that sys.__stdout__ stores the original stdout. The last line restores the sys.__stdout__ original value to such an extent that new print statements will display onscreen, instead of being sent to a file. As additional information, this program uses sys.exit() to quit its execution (refer to Chapter 3 for details). Starting with release 2.0, the print statement can have its output directed to a file-like object, as it is demonstrated in the following example. print >> sys.stderr, "Sorry, you cannot do that!" Formatting OperationsPython provides formatting operations similar to the printf() function from the C language. Take a look at the following example: >>> print "Mr. Lumberjack! do not sing!" What if you don't want to hard-code the name inside the string? Compare the previous line of code against the following one: >>> print "Mr. %s, do not sing!" % someone Flexible, don't you think? And by the way, the order of the elements doesn't affect the final result. Therefore, saying >>> print "Mr. %s" % someone is the same as saying >>> print someone % "Mr. %s" As a matter of fact, the following example shows how Python handles multiple format arguments. Note that you need to provide a tuple of values to fill the position indicated by the formatting operators (see Table 2.2). >>> print "The %s has %i wings" % ("parrot", 2) The following code is another simple example: >>> value = 14 >>> print "The value is %d" % value The value is 14 Next, you will see some special ways to format operations by putting special characters between the % literal and the formatting operator. Before going through the examples, we need to initialize some variables. >>> intg = 42 >>> fltn = 13.142783 >>> strg = "hello" >>> dict = {"xx":13, "yy":1.54321, "zz":"parrot"}
Tip
A * can be used in the place of any number. It uses the next value that matches that format in a tuple. >>> print "%*.*f" % (5,3,2.45) 2.450 Note
Python 2.0 contains a new format string called %r, which prints the repr() value of the given argument. You can clearly see the difference between %r and %s by looking at the following example. '%r %s'% ('Python', 'Python') returns the string 'Python'Python
|
© 2002, O'Reilly & Associates, Inc. |