String Indexing and Negative Indices
A slice with start >= end always produces an empty string, not an error.
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
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.
Equal boundaries: fruit[3:3] has the same start and end index. No range exists between them, so the result is an empty string.
Start 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.
Both 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
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
Treating an empty slice as an error
Empty strings are valid strings with length 0 and can be used like other strings.
Fix:
Check the start and end indices before deciding that the result is incorrect.Expecting fruit[5:2] to reverse characters
The ordinary slice returns empty when the start index comes after the end index.
Fix:
Use a negative step when backward slicing or reversal is intended.Overlooking equal boundaries
The slice selects from index 3 up to, but not including, index 3, leaving no range to select.
Fix:
Recognize 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
- A slice with start equal to end produces an empty string.
- A slice with start greater than end also produces an empty string in the ordinary slice form.
- An empty string has length 0 and contains no characters, but it remains a valid string.
- An ordinary backward-looking slice such as fruit[5:2] does not reverse characters; a negative step is needed for reversal.
- When debugging an empty result, compare the start and end indices and check whether the intended direction was forward or backward.
Key Takeaways
- 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.