Strings are not like integers, floats, and booleans. A string is a sequence, which means it is an ordered collection of other values. In this chapter you’ll see how to access the characters that make up a string, and you’ll learn about some of the methods strings provide.
A string is a sequence of characters. You can access the characters one at a time with the bracket operator:
The second statement selects character number 1 from fruit and assigns it to letter.
The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name).
But you might not get what you expect:
For most people, the first letter of ’banana’
is b, not
a. But for computer scientists, the index is an offset from the
beginning of the string, and the offset of the first letter is zero.
So b is the 0th letter (“zero-eth”) of ’banana’
, a is the 1th letter (“one-eth”), and n is the 2th letter
(“two-eth”).
As an index you can use an expression that contains variables and operators:
But the value of the index has to be an integer. Otherwise you get:
len is a built-in function that returns the number of characters in a string:
To get the last letter of a string, you might be tempted to try something like this:
The reason for the IndexError is that there is no letter in ’banana’ with the index 6. Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, you have to subtract 1 from length:
Or you can use negative indices, which count backward from the end of the string. The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.
A segment of a string is called a slice. Selecting a slice is similar to selecting a character:
The operator [n:m] returns the part of the string from the “n-eth” character to the “m-eth” character, including the first but excluding the last. This behavior is counterintuitive, but it might help to imagine the indices pointing between the characters, as in Figure 14.1.
If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string:
If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:
An empty string contains no characters and has length 0, but other than that, it is the same as any other string.
Continuing this example, what do you think fruit[:] means? Try it and see.