Datasheet

codeEditor can run a Python shell, where you can experiment with simple Python programming lan-
guage statements.
Within the shell, you have learned the basics of how to handle strings, including adding strings together
to create longer strings as well as using format specifiers to insert one or more strings into another string
that has format specifiers. The format specifier
%s is used for strings, and it can be combined with num-
bers, such as
%8s, to specify that you want space for eight characters — no more and no less. In later
chapters, you will learn about other format specifiers that work with other types.
You also learned how to print strings that you have created. Printing is a type of input/output operation
(input/output is covered in more detail in Chapter 8). Using the
print function, you can present users
of your program with strings that you have created.
In the next chapter, you will learn about dealing with simple numbers and the operations that you can
perform on them, as well as how to combine numbers and strings so that
print can render numbers dis-
playable. This technique of using format specifiers will enable you to display other types of data as well.
Exercises
1. In the Python shell, type the string, "Rock a by baby,\n\ton the tree top,\t\twhen the wind
blows\n\t\t\t the cradle will drop." Experiment with the number and placement of the
\t and
\n escape sequences and see how this affects what you see. What do you think will happen?
2. In the Python shell, use the same string indicated in the prior exercise, but display the string
using the
print function. Again, play with the number and location of the \n and \t escape
sequences. What do you think will happen?
11
Programming Basics and Strings
04_596543 ch01.qxd 6/29/05 10:59 PM Page 11