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).
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].
Find the start: The start index is 1, so the character at position 1, Y, is included.
Move toward the stop: The positions after 1 and before 4 are 2 and 3, containing T and H.
Leave 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
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:].
Read [:3]: The omitted start means begin at the beginning. The stop index is 3, so positions 0, 1, and 2 are selected.
Read [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
Including the stop index in the result
The stop index is exclusive, so position 5 is not part of the result.
Fix:
Select positions 0 through 4 for [0:5].Starting one position after the stated start index
The starting index is inclusive, so the character at position 2 must be included.
Fix:
Begin at position 2 and continue through the position before 5.Forgetting what an omitted boundary means
An omitted start means the slice begins at the beginning of the string.
Fix:
Interpret [:m] as starting at the beginning and stopping before m.Stopping early when the stop index is omitted
An omitted stop means the slice continues to the end of the string.
Fix:
Include 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
- Map each character to its integer position.
- Read the first number in [n:m] as the inclusive start index.
- Read the second number as the exclusive stop index.
- Include positions from the start up to, but not including, the stop.
- If 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.