String Indexing with Integer Positions
A slice extracts a substring using [n:m], where n is the starting index (inclusive) and m is the stopping index (exclusive).
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…
A String as Indexed Characters
A string can be understood as a sequence of characters, with each character associated with an integer position called an index. A slice selects a substring by giving a starting index and a stopping index in the form [n:m]. To predict the result, first map each position to its character, then identify the positions covered by the slice.
Reading the Slice Range
In [n:m], n is the starting index and m is the stopping index. The starting index is inclusive, so the character at position n is included. The stopping index is exclusive, so the character at position m is not included. The selected positions are therefore n, n+1, and the positions before m.
Selecting Positions 1 Through 3
Given the string PYTHON, determine the characters selected by [1:4].
- 1Find the start
The start index is 1, so the character at position 1, Y, is included.
- 2Move toward the stop
The positions after 1 and before 4 are 2 and 3, containing T and H.
- 3Leave out the stop
Index 4 contains O, but index 4 is the exclusive stopping position, so O is not selected.
The selected substring is YTH.
What do you think happens?
For the string PYTHON, which characters are selected by [0:5]?
Reveal answer
PYTHO
The slice begins at index 0 and includes positions 0, 1, 2, 3, and 4. The stop index 5 is excluded, so the character at index 5 is not selected.
Why the Stop Is Excluded
The exclusive stop creates a consistent boundary: start at n and continue up to, but not including, m. This makes the number of selected characters easy to describe. A slice written as [i:i+n] selects n consecutive characters, because the positions from i through the position before i+n contain exactly n positions. The exclusive stop is therefore intentional and helps avoid off-by-one confusion when selecting a known number of characters.
Omitting a Boundary
A slice does not have to show both boundaries. If the start index is omitted, as in [:m], the slice begins at the beginning of the string and stops before index m. If the stop index is omitted, as in [n:], the slice begins at index n and continues to the end of the string.
| Notation | Starting point | Stopping point |
|---|---|---|
| [n:m] | Index n | Before index m |
| [:m] | Beginning of the string | Before index m |
| [n:] | Index n | End of the string |
The omitted boundary supplies the beginning or end of the string.
Using an Omitted Boundary
Given the string PYTHON, determine the results of [:3] and [3:].
- 1Read [:3]
The omitted start means begin at the beginning. The stop index is 3, so positions 0, 1, and 2 are selected.
- 2Read [3:]
The start index is 3, and the omitted stop means continue to the end. Positions 3, 4, and 5 are selected.
[:3] selects PYT, and [3:] selects HON.
Predicting Selected Characters
To predict a slice, use a fixed three-step process. First, write the integer position under each character. Second, identify the start position and the position immediately before the stop position. Third, read only those characters in order. For an omitted start, begin with the first character. For an omitted stop, continue through the final character.
For the string NOTEBOOK, predict the result of [2:6], [:4], and [5:].
Hints
- Write the positions under N, O, T, E, B, O, O, K.
- For [2:6], include positions 2, 3, 4, and 5.
- For [:4], begin at the beginning and stop before position 4.
- For [5:], begin at position 5 and continue to the end.
Frequent Boundary Mistakes
Common mistake Including the stop index in the result
What it looks likeTreating [0:5] as selecting positions 0, 1, 2, 3, 4, and 5
Why it failsThe stop index is exclusive, so position 5 is not part of the result.
Do this insteadSelect positions 0 through 4 for [0:5].
Common mistake Starting one position after the stated start index
What it looks likeTreating [2:5] as beginning at position 3
Why it failsThe starting index is inclusive, so the character at position 2 must be included.
Do this insteadBegin at position 2 and continue through the position before 5.
Common mistake Forgetting what an omitted boundary means
What it looks likeReading [:m] as if it had no starting point
Why it failsAn omitted start means the slice begins at the beginning of the string.
Do this insteadInterpret [:m] as starting at the beginning and stopping before m.
Common mistake Stopping early when the stop index is omitted
What it looks likeReading [n:] as if a hidden stop index ended the slice after one character
Why it failsAn omitted stop means the slice continues to the end of the string.
Do this insteadInclude the character at n and every following character through the end.
When uncertain, list the positions explicitly. Treat the left boundary as included and the right boundary as excluded. This simple habit makes the selected characters visible and reduces off-by-one confusion.
Slice Selection Checklist
- 1Map each character to its integer position.
- 2Read the first number in [n:m] as the inclusive start index.
- 3Read the second number as the exclusive stop index.
- 4Include positions from the start up to, but not including, the stop.
- 5If the start is omitted, begin at the beginning; if the stop is omitted, continue to the end.
Key Takeaways
- A slice uses [n:m] to extract a substring.
- The start index n is included, while the stop index m is excluded.
- Omitting the start selects from the beginning, and omitting the stop selects to the end.
- The notation [i:i+n] selects n consecutive characters.
- To predict a result, map positions to characters and read from the inclusive start up to the exclusive stop.