When working with strings, we often need to extract substrings.
For example, if a string contains a phone number, we may want to extract the area
code. In this lecture, I'll introduce two
techniques, indexing and slicing, and we'll use those to extract substrings of
the strings we're working with. In this lecture, we'll use the string
learn to program as our running example. Each character of a string has an index
which, which is it's position in the string.
So, the string at index zero, indicated using this bracket notation, is the L.
At index one of the string, we have an e. At index two, is the first a.
Notice that zero is the first position, not one.
We start counting from zero. We can also use negative indices to count
from the end, or from the right hand side of the string.
So, the string at -one is the m. At -two is the last a,
At -three is the r, the last r. So, once we've seen using an index, we can
extract parts of the string one character at a time. But, we can extract more than
one character using an approach called slicing.
With slicing, we provide a start index followed by a colon, followed by an end
index and that gives us a substring from the start index up to, but not including
the ending index. For example, we can get this string from
position zero, index zero up to, but not including index five, and that gives us
the string Learn. From index six up to, but not including
eight, is the to. And, from index nine up to sixteen, is the
word Program, string Program. Sixteen is equal to the length of the
string. So, an alternative would be to start at
index nine but go up to len of s. Len is a built in function that returns
the length of the string, so this expression is equivalent to the one above.
Another alternative would be to go from nine, put the pole in, and omit the ending
index. In which case, the default is to go to the
end of the string, so this is also equivalent to the two expressions above.