Datasheet

Why the Quotes?
Now, back to strings in particular. Strings are the basic unit of text in Python. Unlike some other pro-
gramming languages, a single letter is represented as a one-letter string. Instead of trying to explain
strings in terms of other concepts in a vacuum, let’s create some examples of strings using the Python
shell and build from there.
Try It Out Entering Strings with Different Quotes
Enter the following strings, keeping in mind the type of quotes (single or double) and the ends of lines
(use the Enter key when you see that the end of a line has been reached):
>>> “This is another string”
‘This is another string’
>>> ‘This is also a string’
‘This is also a string’
>>> “””This is a third string that is some
... how different”””
‘This is a third string that is some\n how different’
How It Works
If you use different quotes, they may look different to you; to the Python interpreter; however all of
them can be used in the same situations and are very similar. For more information, read on.
These examples raise a few questions. In your first text example, you saw that the text was enclosed
in double quotes, and when python saw two quotes it repeated those double quotes on the next line.
However, in the preceding example, double quotes are used for “This is another string”, but below it
single quotes are used. Then, in the third example, three double quotes in a row are used, and after the
word “some” we used the Enter key, which caused a new line to appear. The following section explains
these seemingly arbitrary conventions.
Understanding Different Quotes
Three different types of quotes are used in Python. First, there are the single and double quotes, which
you can look at in two ways. In one way, they are identical. They work the same way and they do the
same things. Why have both? Well, there are a couple of reasons. First, strings play a huge part in almost
any program that you’re going to write, and quotes define strings. One challenge when you first use
them is that quotes aren’t special characters that appear only in computer programs. They are a part of
any normal English text to indicate that someone has spoken. In addition, they are used for emphasis or
to indicate that something is literally what was seen or experienced.
The dilemma for a programming language is that when you’re programming, you can only use charac-
ters that are already on a keyboard. However, the keys on a keyboard can be entered by the average user,
so obviously people normally use those keys for tasks other than programming! Therefore, how do you
make it a special character? How do you indicate to the language that you, the programmer, mean some-
thing different when you type a set of quotes to pass a string to your program, versus when you, as the
programmer, enter quotes to explain something to the person using your program?
One solution to this dilemma is a technique that’s called escaping. In most programming languages, at
least one character, called an escape character, is designated; and it has the power to remove the special
6
Chapter 1
04_596543 ch01.qxd 6/29/05 10:59 PM Page 6