Negative Indexing in Strings
A string is a sequence of individual characters that can be accessed one at a time using the bracket operator.
One Character at a Time
A string is a sequence of individual characters. Python lets you access one character at a time by placing an integer index inside brackets after the string name. The main idea is to connect each index with the character occupying that position in the sequence.
Positions from the Start
Python uses zero-based indexing. That means the first character has index 0, the next character has index 1, and later characters continue with increasing index values. An index represents an offset from the start of the string, so the first character is zero positions away from the start.
Finding the First Character
Suppose the string is Code. Which index selects its first character?
Locate the start: The first character is at the beginning of the sequence, so its zero-based index is 0.
Apply the bracket operator: Use the string name followed by brackets containing the integer index 0. This selects the character C.
Avoid index 1: Index 1 refers to the next character, o, not the first character.
The first character is selected with index 0, and index 1 selects the second character.
Positions from the End
Negative indexing provides another way to identify characters: count backward from the end of the string. The last character is at index -1, the character immediately before it is at index -2, and the count continues toward the beginning. Positive and negative indices can therefore point to the same character from opposite directions.
Selecting the Last Character
Suppose the string is Code. Which index selects its last character?
Start at the end: The last character is identified by beginning the backward count at -1.
Match the character: For Code, index -1 selects e.
Compare directions: The same character is reached from the start with positive index 3 and from the end with negative index -1.
The last character can be selected with negative index -1.
Bracket Selection
The bracket operator uses the form string_name[index]. The index must be an integer. Replace string_name with the name of the string and index with the position you want to select. A non-integer index, such as a float, raises a TypeError.
Fixing the First-Character Error
The most common indexing mistake is assuming that the first character is at index 1. In Python, index 1 means one position after the start, so it identifies the second character. To select the first character, use index 0.
Treating index 1 as the first character
Python starts counting at index 0, so index 1 is the second position.
Fix:
Use index 0 when selecting the first character.Using a non-integer index
The bracket operator requires the index to be an integer.
Fix:
Use an integer index, such as 0, 1, -1, or -2.Forgetting which end a negative index starts from
Negative indexing begins at the end, where -1 identifies the last character.
Fix:
Count backward from the end for negative indices.
Check Your Index
What do you think happens?
For the string Code, which character is selected by index -2?
Reveal answer
Answer: d
Negative indices count backward from the end. Index -1 selects e, so index -2 selects the character immediately before it, d.
For the string Learn, identify the character selected by positive index 0, positive index 1, negative index -1, and negative index -2. Then explain why positive index 1 is not the first character.
Hints
- Write the characters in order and place positive indices from left to right.
- For negative indices, begin at the final character and count backward.
- Remember that the first positive index is 0.
Key Takeaways
- A string is a sequence of individual characters that can be accessed one at a time.
- Python uses zero-based indexing, so the first character is at index 0 and the second character is at index 1.
- The bracket operator has the form string_name[index], and the index must be an integer.
- Positive indices count from the beginning, while negative indices count backward from the end, with -1 identifying the last character.
- When selecting the first character, replace the common but incorrect index 1 with index 0.