Fundamentals of Python - First Programs, 2nd Ed
Fundamentals of Python - First Programs, 2nd Ed
(continued )
This driver allows the programmer not only to verify the two tasks, but also to obtain
some data to use when testing the complete program later on. For example, the
programmer can supply a text file that contains the number of sentences, words, and
syllables already tested in the driver, and then compare the two test results.
In bottom-up testing, the lower-level tasks must be developed and tested before those
tasks that depend on the lower-level tasks.
When you have tested all of the parts, you can integrate them into the complete
program. The test data at that point should be short files that produce the expected
results. Then, you should use longer files. For example, you might see if plaintext ver-
sions of Dr. Seuss’s Green Eggs and Ham and Shakespeare’s Hamlet produce grade
levels of 5th grade and 12th grade, respectively. Or you could test the program with
its own source program file—but we predict that its readability will seem quite low,
because it lacks most of the standard end-of-sentence marks!
Summary
•• A string is a sequence of zero or more characters. The len function returns the number
of characters in its string argument. Each character occupies a position in the string.
The positions range from 0 to the length of the string minus 1.
•• A string is an immutable data structure. Its contents can be accessed, but its structure
cannot be modified.
•• The subscript operator [] can be used to access a character at a given position in a
string. The operand or index inside the subscript operator must be an integer expres-
sion whose value is less than the string’s length. A negative index can be used to access a
character at or near the end of the string, starting with –1.
•• A subscript operator can also be used for slicing—to fetch a substring from a string.
When the subscript has the form [<start>:], the substring contains the characters
from the start position to the end of the string. When the form is [:<end>], the posi-
tions range from the first one to end – 1. When the form is [<start>:<end>], the posi-
tions range from start to end – 1.
Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.