## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

“91974˙CH03˙ﬁnal” 2012/12/14 14:15 page 83 #1
CHAPTER
3
Codes and Other Secrets
3.1 Objectives
To introduce the string data type
To demonstrate the use of string methods and operators
To introduce simple cryptographic algorithms
3.2 Introduction
For almost as long as people have been writing, people have also been trying to hide
what they were writing from others. Herodotus describes the use of “secret writing” that
saved Greece from being conquered by Xerxes and the Persians. The Kama Sutra, based
on writings dating back to the fourth century bce, instructs women in the art of secret
writing in order to hide the details of their liaisons. In this chapter we explore some simple
forms of secret writing using Python.
Today this art of secret writing is called cryptography. You use cryptography nearly
every day without even thinking about it. Not because you are sending secret messages to
your friends, (although there are several cryptographic add-ons for email these days) but
whenever you use your browser. When you make a purchase on the Web or check your
grades or bank online, you are using cryptography.
Before we dive into cryptography we must learn about another important Python data
type called the string—a data structure that allows us to represent the written word in
our programs. After the numeric data types introduced in “Introduction” (Chapter 1), the
next most common type of data used in programming is the string.
83
“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
interesting purposes.
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
operators.
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
the outside.
>>> "hello"
'hello'
>>> 'world'
'world'
>>> a = "hello"
>>> a
'hello'
>>> b = 'world'
>>> "let's go"
"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
“91974˙CH03˙ﬁnal” 2012/12/14 14:15 page 85 #3
3.2 Introduction 85
3.2.1 Concatenation
As a ﬁrst example of an operator applied to a string, we will look at the addition operator
(+). When + is applied to two strings, we call it the concatenation operator. When you
concatenate two strings, you simply put the two strings one after the other, as shown in
Session 3.2. Notice that the concatenation operator does not automatically add a space
between two strings. To add a space, you must concatenate a string that contains only a
space.
>>> "hello " + "world!"
'hello world!'
>>> fname = 'John'
>>> lname = 'Smith'
>>> fname + lname
'JohnSmith'
>>> fullName = fname+''+lname
>>> fullName
'John Smith'
>>>
Session 3.2 String concatenation
3.2.2 Repetition
The next operator we can apply to a string is the repetition operator (*). As you might
guess, this operator takes a string and repeats it as many times as you would like. For
example, suppose you want to create the string 'gogogo'. Rather than typing ‘go’ multiple
times, you can simply apply the repetition operator as follows: 'go'*3
As with their numeric counterparts, the repetition operator has a higher precedence than
concatenation. We can thus construct strings using both operators together, as shown in
Session 3.3.

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required