Datasheet

In the second string, the Q. is stranded in the middle, with John and Public far to either side. The
behavior on its right-hand side has just been explained. The behavior on its left happens for very sim-
ilar reasons. An area with 10 spaces has been created in the string, but this string was specified with a
%-10s. The - in that specifier means that the item should be pushed to the left, instead of to the right,
as it would normally.
Displaying Strings with Print
Up until now, you have seen how Python represents the strings you type, but only how it represents
them internally. However, you haven’t actually done anything that your program would show to a user.
The point of the vast majority of programs is to present users with information — programs produce
everything from sports statistics to train schedules to web pages to automated telephone voice response
units. The key point is that they all have to make sense to a person eventually.
Try It Out Printing Text with Print
For displaying text, a special feature is built into useful languages, one that helps the programmer dis-
play information to users. The basic way to do this in Python is by using the
print function:
>>> print “%s %s %10s” % (“John”, “Q.”, “Public”)
John Q. Public
>>>
You’ll notice that there are no longer any quotes surrounding the first, middle, and last name. In this
case, it’s significant — this is the first thing that you’ve done that would actually be seen by someone
using a program that you’ve written!
How It Works
print is a function — a special name that you can put in your programs that will perform one or more
tasks behind the scenes. Normally, you don’t have to worry about how it happens. (When you start writ-
ing your own functions in Chapter 5, you’ll naturally start to think more about how this works.)
In this case, the
print function is an example of a built-in function, which is a function included as a
part of Python, as opposed to a function that you or another programmer has written. The
print func-
tion performs output — that is, it presents something to the user using a mechanism that they can see,
such as a terminal, a window, a printer, or perhaps another device (such as a scrolling LED display).
Related routines perform input, such as getting information from the user, from a file, from the network,
and so on. Python considers these input/output (I/O) routines. I/O commonly refers to anything in a
program that prints, saves, goes to or from a disk, or connects to a network. You will learn more about
I/O in Chapter 8.
Summary
In this chapter, you’ve begun to learn how to use the programming editor codeEditor, which is a pro-
gram written in Python for the purpose of editing Python programs. In addition to editing files,
10
Chapter 1
04_596543 ch01.qxd 6/29/05 10:59 PM Page 10