|
CS 140: Introduction
to Computer Programming
Tests and Loops
on Strings
It is possible to check whether or not a
particular character appears within a given
string, as illustrated by the following code.
if "b" in word:
print "Your word contains the
letter b."
if "a" not in word and "e" not in word:
print "Your word doesn't have an
a or an e."
This construction even works for more than one
character. For instance, supposing that word = "exasperation"
then the test if
"rat" in word: would be
true.
Python provides several built-in commands for
"cleaning up" strings. Thus if a program
asks for a word and the user types House
(with an extra space at the end) then the
command len(word)
would return 6, since that space counts as a
character. To eliminate extra spaces from
the start and end of a string, use word = word.strip().
Similarly, the condition if
word[0] == "h" would not
correctly identify that the first letter was an
h, since the user happened to capitalize the
word. One way to avoid this difficulty is
to first type word
= word.lower(), which
changes all letters to lowercase.
Similarly, word
= word.upper() will
change the letters to all capitals.
There is a handy method to step through the
characters within a string one by one using a
special type of for loop. For instance, to
print a word vertically, one could type
word = "help"
for
letter in
word:
print letter
to obtain the output
h
e
l
p
This sort of loop can also be used to reverse
the order of the letters within a word.
The basic idea is to initialize a new variable
to be the "empty string", then gradually add
letters of the original word to the front
of the new word. We'll give the result in
all capitals.
word = raw_input("Please enter your favorite
word: ")
word = word.upper()
reverse = ""
for
letter in
word:
reverse = letter +
reverse
print "The reverse of your word
is", reverse+"."
There is a second method for moving through the
characters within a string one at a time that
gives the programmer a little more
control. The idea is to use a regular for
loop that steps through all position values
within the word, like this.
word = "caterpillar"
for j in range(0,len(word)):
print " "*j+word[j]
The variable j will be
equal to 0, 1, 2, ..., 10, so word[j]
will give each successive letter within the word
in turn. In this particular program some
white space is printed before each letter: no
spaces before the c, one
space before the a, two
spaces before the t, all
the way up to ten spaces before the r.
In other words, it will print the word
diagonally!
As another example of this type of for loop,
suppose that we had a string called page
that contained an entire page of text. We
could count how many times the word "cow"
appeared within the text by going to each
position within the string, then testing whether
the next three characters, beginning at that
position, were "cow".
count = 0
for j in range(0,len(word)):
if page[j:j+3] == "now":
count = count + 1
print "There were", count,
"cows on the page."
|
|
|