Page 78 -
P. 78
textual data
But how do you get at more than
one character?
For Starbuzz, you don’t just need a single character. You need to extract
the price from the string of HTML, and the price is made up of several
characters.
You need to extract a smaller substring from a bigger string. A substring
is a sequence of characters contained within another string. Specifying
substrings in Python is a little like reading single characters from a string, This will read the smaller
except that you provide two index values within the square brackets: substring from the entire
string contained within “s".
If you provide a single index
after the variable name, you s[14] s[138:147]
get a single character. If you provide two index values, you extract a
group of characters from the first index up to
(but not including) the second index.
Let’s work out what each of the following substring specifications
mean. Imagine the variable s is set to the string below. Your job
is to determine what each of the substrings provide.
s[5:9]
s[10:12]
s[13:18]
Write down what you think “a"
and “b" represent here.
In general, if you specify a substring using s[a:b], then:
a is b is
you are here 4 43