“91974˙CH03˙ﬁnal” — 2012/12/14 — 14:15 — page 84 — #2
84 CHAPTER 3 Codes and Other Secrets
You are already more familiar with strings than you think. Strings are simply sequences
of characters, such as the letters of the alphabet and all the other symbols commonly
used in writing. Most often, these sequences of characters are put together to form familiar
words, but as we will see in this chapter, character sequences can be used for many other
In Python you will know that an object is a string because it is surrounded by either single
quotes (') or double quotes ("). Like numbers, strings are objects that can be named by
variables. Python Session 3.1 illustrates a number of simple strings.
Like the numeric data types, Python provides us with operators we can use on strings to
transform them. In the next four sections we will examine some commonly used string
Strings can contain any characters: letters, numbers, punctuation marks, and even quota-
tion marks. When you want to have a string that contains a single quote, you use double
quotes around the outside of the string. When you want to have double quotes inside your
string, you use single quotes around the outside. If your string does not contain either
single or double quotes, it does not matter whether you use single or double quotes around
>>> a = "hello"
>>> b = 'world'
>>> "let's go"
>>> 'she said "how are you?" then left'
'she said "how are you?" then left'
Session 3.1 A variety of simple strings