Slicing with Steps and Negative Steps
An empty slice result is a valid string result. It is not a slicing 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…
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 |
How start and end relationships affect ordinary slices and backward traversal
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].
- 1fruit[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.
- 2fruit[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.
- 3fruit[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.
Common mistake Assuming fruit[5:2] reverses the characters between indices 5 and 2.
What it looks likefruit[5:2]Why it failsAn ordinary slice does not automatically count backward. With the start after the end and no negative step, the source example produces an empty string.
Do this insteadUse a negative step when the intended operation is backward traversal or reversal.
Common mistake Treating an empty slice as an error.
What it looks likefruit[3:3]Why it failsEqual bounds describe an empty range, so Python returns a valid empty string.
Do this insteadHandle the result as a string whose length is 0.
Common mistake Assuming an index beyond the string's length automatically causes a failure.
What it looks likefruit[7:7]Why it failsThe source example shows that equal bounds beyond the string still follow the empty-range rule.
Do this insteadCheck 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.
- 1fruit[3:3]
Empty: the bounds are equal.
- 2fruit[5:2]
Empty: the start is after the end and no negative step is supplied.
- 3fruit[1:4]
Non-empty: the slice describes a forward range.
- 4fruit[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
- 1Equal start and end positions produce an empty string.
- 2With an ordinary slice, a start after the end produces an empty string rather than an error.
- 3A negative step is needed when the intended movement is backward or when reversing is the goal.
- 4An empty string has length 0, contains no characters, and still behaves like a normal string.
- 5To debug an empty result, trace the start, end, and step together.
At a glance
- 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.