7 Strings
Chapter 8 StringsStrings are not like integers, floats, and booleans. A string 8.1 A string is a sequence
>>> fruit = 'banana' >>> letter = fruit[1]
The second statement selects character number 1 from fruit and assigns it to letter. The expression in brackets is called an index. But you might not get what you expect: >>> letter 'a'
For most people, the first letter of >>> letter = fruit[0] >>> letter 'b'
So b is the 0th letter (“zero-eth”) of As an index you can use an expression that contains variables and >>> i = 1 >>> fruit[i] 'a' >>> fruit[i+1] 'n' But the value of the index has to be an integer. Otherwise you >>> letter = fruit[1.5] TypeError: string indices must be integers 8.2 lenlen is a built-in function that returns the number of characters >>> fruit = 'banana' >>> len(fruit) 6
To get the last letter of a string, you might be tempted to try something >>> length = len(fruit) >>> last = fruit[length] IndexError: string index out of range
The reason for the IndexError is that there is no letter in ’banana’ with the index 6. Since we started counting at zero, the >>> last = fruit[length-1] >>> last 'a'
Or you can use negative indices, which count backward from 8.3 Traversal with a for loopA lot of computations involve processing a string one character at a index = 0
while index < len(fruit):
letter = fruit[index]
print(letter)
index = index + 1
This loop traverses the string and displays each letter on a line by As an exercise, write a function that takes a string as an argument Another way to write a traversal is with a for loop: for letter in fruit:
print(letter)
Each time through the loop, the next character in the string is assigned The following example shows how to use concatenation (string addition) prefixes = 'JKLMNOPQ'
suffix = 'ack'
for letter in prefixes:
print(letter + suffix)
The output is: Jack Kack Lack Mack Nack Oack Pack Qack
Of course, that’s not quite right because “Ouack” and “Quack” are 8.4 String slicesA segment of a string is called a slice. Selecting a slice is >>> s = 'Monty Python' >>> s[0:5] 'Monty' >>> s[6:12] 'Python'
The operator [n:m] returns the part of the string from the
If you omit the first index (before the colon), the slice starts at >>> fruit = 'banana' >>> fruit[:3] 'ban' >>> fruit[3:] 'ana'
If the first index is greater than or equal to the second the result >>> fruit = 'banana' >>> fruit[3:3] ''
An empty string contains no characters and has length 0, but other Continuing this example, what do you think 8.5 Strings are immutableIt is tempting to use the [] operator on the left side of an >>> greeting = 'Hello, world!' >>> greeting[0] = 'J' TypeError: 'str' object does not support item assignment
The “object” in this case is the string and the “item” is The reason for the error is that >>> greeting = 'Hello, world!' >>> new_greeting = 'J' + greeting[1:] >>> new_greeting 'Jello, world!'
This example concatenates a new first letter onto 8.6 SearchingWhat does the following function do? def find(word, letter):
index = 0
while index < len(word):
if word[index] == letter:
return index
index = index + 1
return -1
In a sense, find is the inverse of the [] operator. This is the first example we have seen of a return statement If the character doesn’t appear in the string, the program This pattern of computation—traversing a sequence and returning As an exercise, modify find so that it has a 8.7 Looping and countingThe following program counts the number of times the letter a word = 'banana'
count = 0
for letter in word:
if letter == 'a':
count = count + 1
print(count)
This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then
Then rewrite the function so that instead of 8.8 String methodsStrings provide methods that perform a variety of useful operations. Instead of the function syntax upper(word), it uses >>> word = 'banana' >>> new_word = word.upper() >>> new_word 'BANANA'
This form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, word. The empty parentheses indicate that this method takes no A method call is called an invocation; in this case, we would As it turns out, there is a string method named find that >>> word = 'banana'
>>> index = word.find('a')
>>> index
1
In this example, we invoke find on word and pass Actually, the find method is more general than our function; >>> word.find('na')
2
By default, find starts at the beginning of the string, but >>> word.find('na', 3)
4
This is an example of an optional argument; >>> name = 'bob'
>>> name.find('b', 1, 2)
-1
This search fails because b does not 8.9 The in operatorThe word in is a boolean operator that takes two strings and >>> 'a' in 'banana' True >>> 'seed' in 'banana' False
For example, the following function prints all the def in_both(word1, word2):
for letter in word1:
if letter in word2:
print(letter)
With well-chosen variable names, Here’s what you get if you compare apples and oranges: >>> in_both('apples', 'oranges')
a
e
s
8.10 String comparisonThe relational operators work on strings. To see if two strings are equal: if word == 'banana':
print('All right, bananas.')
Other relational operations are useful for putting words in alphabetical if word < 'banana':
print('Your word, ' + word + ', comes before banana.')
elif word > 'banana':
print('Your word, ' + word + ', comes after banana.')
else:
print('All right, bananas.')
Python does not handle uppercase and lowercase letters the same way Your word, Pineapple, comes before banana.
A common way to address this problem is to convert strings to a 8.11 DebuggingWhen you use indices to traverse the values in a sequence, def is_reverse(word1, word2):
if len(word1) != len(word2):
return False
i = 0
j = len(word2)
while j > 0:
if word1[i] != word2[j]:
return False
i = i+1
j = j-1
return True
The first if statement checks whether the words are the i and j are indices: i traverses word1 If we test this function with the words “pots” and “stop”, we >>> is_reverse('pots', 'stop')
...
File "reverse.py", line 15, in is_reverse
if word1[i] != word2[j]:
IndexError: string index out of range
For debugging this kind of error, my first move is to while j > 0:
print(i, j) # print here
if word1[i] != word2[j]:
return False
i = i+1
j = j-1
Now when I run the program again, I get more information: >>> is_reverse('pots', 'stop')
0 4
...
IndexError: string index out of range
The first time through the loop, the value of j is 4, If I fix that error and run the program again, I get: >>> is_reverse('pots', 'stop')
0 3
1 2
2 1
True
This time we get the right answer, but it looks like the loop only ran
I took some license by arranging the variables in the frame Starting with this diagram, run the program on paper, changing the 8.12 Glossary
8.13 ExercisesExercise 1
Read the documentation of the string methods at The documentation uses a syntax that might be confusing. Exercise 2
There is a string method called count that is similar Exercise 3
A string slice can take a third index that specifies the “step >>> fruit = 'banana' >>> fruit[0:5:2] 'bnn' A step size of -1 goes through the word backwards, so Use this idiom to write a one-line version of Exercise 4
The following functions are all intended to check whether a def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
Exercise 5
To rotate a word, rotate each letter by the same amount. Write a function called You might want to use the built-in function ord, which converts >>> ord('c') - ord('a')
2
Because Potentially offensive jokes on the Internet are sometimes encoded in |
ContributeIf you would like to make a contribution to support my books, Are you using one of our books in a class? We’d like to know |




