Slicing with Steps and Negative Steps
A slice with start >= end always produces an empty string, not an error.
The Empty Result
A slice usually extracts characters between two positions. Sometimes, however, there is no range to extract. When the start and end positions are the same, the result is an empty string. In the source example, fruit[3:3] selects from index 3 up to, but not including, index 3, so it selects nothing. A slice whose start comes after its end, such as fruit[5:2], also produces an empty string rather than an error.
An empty slice result is a valid string result. It is not a slicing error.
Tracing the Visited Positions
To understand an empty result, trace three pieces of information: the start position, the end position, and the direction indicated by the step. With ordinary forward slicing, Python tries to move from the start toward the end. If the positions are equal, there is no distance to travel. If the start is after the end, the forward slice cannot select a forward range, so the result is empty.
The diagram uses banana as the traced string. Positions 0 through 6 are shown, with 6 representing the position one past the last character. A forward slice can select positions between a lower start and a higher end. A negative step is the mechanism to use when the intended movement is backward. Without that negative step, fruit[5:2] is treated as an empty forward slice, not as a reversal.
Equal and Reversed Bounds
| Slice relationship | Result | Reason |
|---|---|---|
| start equals end | Empty string | There is no range between the two positions |
| start comes after end in an ordinary slice | Empty string | The requested forward range has no selectable characters |
| start is before end in an ordinary slice | Characters may be selected | There is a forward range to traverse |
| Backward traversal is intended | Use a negative step | A negative step is needed to request movement toward lower positions |
The important distinction is between an ordinary slice whose bounds are in the wrong order and a slice that explicitly requests backward movement. fruit[5:2] is the source example of the first situation: it returns an empty string. A negative step is the appropriate tool when the goal is to move backward or reverse a string. Therefore, do not interpret every pair of reversed bounds as an automatic reversal.
Worked Slice Traces
Tracing three empty slices
Predict the result of each operation on the string banana: fruit[3:3], fruit[5:2], and fruit[7:7].
fruit[3:3]: The start and end positions are equal. The slice has no range from which to select characters, so the result is an empty string.
fruit[5:2]: The start comes after the end, but no negative step is supplied. The ordinary slice does not count backward, so the result is an empty string.
fruit[7:7]: The two positions are equal even though they are beyond the string's length. The same empty-range rule applies, so the result is an empty string.
All three operations produce ''. They produce a valid empty string, not an exception.
True
True
TrueThe third case is useful because it separates two ideas: an index can be beyond the string's length, and a slice can still produce a valid result. In this case, the equal bounds mean there is no range to select, so the result is empty. The source also gives a non-empty forward example, pyt, to contrast with these empty cases.
Empty String Behavior
An empty string is a string containing zero characters. Its length is 0, but it remains a normal string value.
Because an empty string behaves like any other string, you can concatenate it, compare it, iterate over it, and pass it to functions. Iterating over it performs no iterations because there are no characters to visit. This uniform behavior means that an empty slice does not require a special kind of string handling.
0
textDebugging Empty Results
When a slice unexpectedly produces no characters, write down its three controls: start, end, and step. Check whether the start equals the end. Then check whether the start comes after the end. Finally, check whether the operation is meant to move backward and therefore needs a negative step. This trace turns an apparently mysterious empty result into a predictable consequence of the slice bounds and direction.
Assuming fruit[5:2] reverses the characters between indices 5 and 2.
An ordinary slice does not automatically count backward. With the start after the end and no negative step, the source example produces an empty string.
Fix:
Use a negative step when the intended operation is backward traversal or reversal.Treating an empty slice as an error.
Equal bounds describe an empty range, so Python returns a valid empty string.
Fix:
Handle the result as a string whose length is 0.Assuming an index beyond the string's length automatically causes a failure.
The source example shows that equal bounds beyond the string still follow the empty-range rule.
Fix:
Check the relationship between the bounds and the step before assuming the slice failed.
Practice Check
For each expression, predict whether the result is empty or non-empty before checking the answer: fruit[3:3], fruit[5:2], fruit[1:4], and fruit[7:7]. Explain each prediction using the relationship between start and end.
Hints
- Equal start and end positions leave no range to select.
- A start after the end produces an empty ordinary slice when no negative step is used.
- A start before the end gives a forward range that can contain characters.
- Bounds beyond the string do not change the equal-bound rule.
Practice answers
Classify fruit[3:3], fruit[5:2], fruit[1:4], and fruit[7:7] as empty or non-empty.
fruit[3:3]: Empty: the bounds are equal.
fruit[5:2]: Empty: the start is after the end and no negative step is supplied.
fruit[1:4]: Non-empty: the slice describes a forward range.
fruit[7:7]: Empty: the bounds are equal, even though they are beyond the string.
The expected classifications are empty, empty, non-empty, and empty.
Key Takeaways
- Equal start and end positions produce an empty string.
- With an ordinary slice, a start after the end produces an empty string rather than an error.
- A negative step is needed when the intended movement is backward or when reversing is the goal.
- An empty string has length 0, contains no characters, and still behaves like a normal string.
- To debug an empty result, trace the start, end, and step together.
Key Takeaways
- A slice with equal bounds has no characters to select and returns an empty string.
- An ordinary slice with its start after its end also returns an empty string, not an error.
- Backward slicing requires a negative step; reversed bounds alone do not reverse a string.
- Empty strings are valid strings with length 0 and no characters.
- Tracing start, end, and step is an effective way to debug empty slice results.