String Indexing and Negative Indices
A slice with start >= end always produces an empty string, not an error.
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…
When a Slice Selects Nothing
A string slice is expected to extract characters between two positions. The important edge case is what happens when the selected range has no forward characters: Python returns an empty string. This is a valid result, not an error.
What do you think happens?
What is the result of fruit[3:3] when fruit is the string banana?
Reveal answer
An empty string
The slice starts at index 3 and stops before index 3. There is no range of characters to select, so the result is empty.
Tracing the Slice Boundaries
The start and end indices determine the range being considered. A slice with equal boundaries, such as fruit[3:3], has no characters between its start and end. A slice whose start is later than its end, such as fruit[5:2], also produces an empty string. Python does not automatically count backward for this form of slicing.
Three Empty Slice Results
Predict the results of the following slices of banana: fruit[3:3], fruit[5:2], and a slice whose start and end are both beyond the string's length.
- 1Equal boundaries
fruit[3:3] has the same start and end index. No range exists between them, so the result is an empty string.
- 2Start after end
fruit[5:2] begins after its ending position. In this slicing form, Python does not reverse the characters; it returns an empty string.
- 3Both boundaries past the string
A slice whose start and end are both beyond the string's length follows the same empty-range rule when the start equals the end.
All three results are empty strings.
What Empty Means in Python
An empty string is a valid string with length 0 and no characters. It behaves like any other string: it can be concatenated, compared, iterated over, and passed to functions. Iterating over it does nothing because there are no characters to process.
Recognizing Reversal Mistakes
fruit[5:2]Empty string: the start index is greater than the end index, so the ordinary slice does not count backward.
fruit[5:2] with a negative step
A negative step is the required direction when the intention is to reverse or move backward.
The expression fruit[5:2] may look as though it should return characters in reverse order. It does not. With the ordinary slice form described here, a start index greater than the end index produces an empty string. Reversal requires a negative step.
Debugging Empty Results
Common mistake Treating an empty slice as an error
What it looks likeA slice returns an empty string, so the program is assumed to have failed.
Why it failsEmpty strings are valid strings with length 0 and can be used like other strings.
Do this insteadCheck the start and end indices before deciding that the result is incorrect.
Common mistake Expecting fruit[5:2] to reverse characters
What it looks likeThe start index is 5 and the end index is 2, so the result is expected to contain characters in reverse order.
Why it failsThe ordinary slice returns empty when the start index comes after the end index.
Do this insteadUse a negative step when backward slicing or reversal is intended.
Common mistake Overlooking equal boundaries
What it looks likefruit[3:3] is expected to contain the character at index 3.
Why it failsThe slice selects from index 3 up to, but not including, index 3, leaving no range to select.
Do this insteadRecognize start equals end as an empty-range case.
When a slice unexpectedly returns an empty string, first compare the start and end indices. If they are equal, there is no range. If the start is greater, the ordinary slice is not moving backward. Then check whether the intended operation was selection in the forward direction or reversal.
Predicting Slice Outcomes
Predict each result before checking it: fruit[3:3], fruit[5:2], a forward slice that produces pyt, a slice whose equal start and end are both past the string, and a forward slice from index 5 to 6.
Hints
- First compare the start and end indices.
- Equal boundaries produce an empty string.
- A start index greater than the end index produces an empty string in the ordinary slice form.
- A forward range from index 5 to 6 is not empty.
| Slice situation | Result | Reason |
|---|---|---|
| start equals end | empty string | There is no range between the boundaries. |
| start exceeds end | empty string | The ordinary slice does not count backward. |
| forward range | pyt | The range contains characters. |
| equal boundaries beyond the string | empty string | The start and end still match, so the selected range is empty. |
| forward range from index 5 to 6 | n | The forward range contains one character. |
Expected results from the source examples
Key Takeaways
- 1A slice with start equal to end produces an empty string.
- 2A slice with start greater than end also produces an empty string in the ordinary slice form.
- 3An empty string has length 0 and contains no characters, but it remains a valid string.
- 4An ordinary backward-looking slice such as fruit[5:2] does not reverse characters; a negative step is needed for reversal.
- 5When debugging an empty result, compare the start and end indices and check whether the intended direction was forward or backward.
At a glance
- Equal or reversed slice boundaries produce an empty string rather than an error.
- Empty strings have length 0, contain no characters, and still behave like strings.
- A negative step is needed when the intention is to move backward or reverse characters.
- Comparing the start and end indices is the fastest way to diagnose an unexpected empty slice.