8.2. Bracket Notation¶
Strings are ordered collections of characters, and this gives us a nice mental model for how they are put together. Each character has its own specific location within a string. We call this location the index.
Consider the string
'Go Python!' The first character,
'G', has an index
value of 0, the first
'o' has index 1, the space
' ' has index 2, and
so on. Index values are integers representing the position of a character
within a given string.
Note that this is another example of zero-based indexing. The count begins with the value 0.
8.2.1. Access One Character¶
Bracket notation is the special syntax that allows us to access the single characters that make up a string. To access a character, we use the syntax:
index is the position of the character we want.
'school' selects the character at index
2 and creates
a new string containing just that one character.
With zero-based indexing, the letter at index 0 of
at position  we have the letter
Predict which letters will be printed to the console. Check your answers by
clicking Run. Remember that with zero-based indexing, the first
character always has an index value of
1 2 3 4 5
this_string = 'Zero-based indexing!' print(this_string) print('Alphabet soup')
Now try the following:
- Change the index values in lines 3 and 5 to see how they affect the output.
40into the brackets in line 3. What happens when you use an index value that is larger than the length of the string?
40inside the brackets with a negative number, like
-1. What happens?
In step 2 above,
this_string causes an index out of range error.
This happens anytime we try to reference an index location that does not exist
in the string.
We will discuss what a negative index value returns in step 3 shortly.
18.104.22.168. Expressions for
If we want to access the last character in a string, we need to know its index value. How can we find this number without having to count all of the characters?
An index value must either be an integer—like 0, 1, 2, etc.—or an expression that evaluates to an integer.
Recall that we can use the
len() function to return the number of
characters in a string.
1 2 3
this_string = 'Zero-based indexing!' print(len(this_string))
len(this_string) evaluates to 20, and that value gets printed to the
In the Try It example above, replace
Wait…what? We got an index out of range error, but we KNOW that
this_string is 20 characters long!
The reason is, once again, zero-based indexing. Since we start counting index
0, the 20th character has an index value of
We can access the last character of the string and avoid the out of range error by using:
print(this_string[len(this_string) - 1])
len(this_string) - 1 evaluates to
this_string is the last character (
8.2.2. Negative Index Values¶
Consider the string
'Go Python!' again. From left to right, the characters
take the index values 0 - 9.
Python also allows us to use index values that move from the end of the string to the beginning (right to left). In this case, the index values are negative integers.
Note that when we move from right to left, the index values start with
22.214.171.124. Try It!¶
Return to the repl above and do the following:
- Use a negative index to print the character
'x'from the string
- What is the smallest negative number that gives an index out of range error? In this case, “smallest” means “closest to zero”.
this_string[len(this_string)]throws an error. What about
8.2.3. Check Your Understanding¶
phrase = 'Code for fun', then
phrase evaluates to:
Which of the following returns
my_str = 'index'? Choose
ALL correct answers.
my_str == 'n'
my_str == 'x'
my_str == ' '
my_str == 'i'
What is printed by the following code?
phrase = "Python rocks!" print(phrase[len(phrase) - 9])
my_str = 'ABC DEF GHI', which of the following