Negative Indexing in Strings
A string is a sequence of individual characters that can be accessed one at a time using the bracket operator.
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
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?
- 1Locate the start
The first character is at the beginning of the sequence, so its zero-based index is 0.
- 2Apply the bracket operator
Use the string name followed by brackets containing the integer index 0. This selects the character C.
- 3Avoid 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?
- 1Start at the end
The last character is identified by beginning the backward count at -1.
- 2Match the character
For Code, index -1 selects e.
- 3Compare 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.
Common mistake Treating index 1 as the first character
What it looks likeFor the string Code, using index 1 selects o.
Why it failsPython starts counting at index 0, so index 1 is the second position.
Do this insteadUse index 0 when selecting the first character.
Common mistake Using a non-integer index
What it looks likeTrying to use a float as the index.
Why it failsThe bracket operator requires the index to be an integer.
Do this insteadUse an integer index, such as 0, 1, -1, or -2.
Common mistake Forgetting which end a negative index starts from
What it looks likeTreating -1 as the first character.
Why it failsNegative indexing begins at the end, where -1 identifies the last character.
Do this insteadCount 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
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.