String Traversal and Iteration
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 intended to extract characters between two positions. The important edge case is what happens when the starting position does not come before the ending position. In Python, a slice with start greater than or equal to end produces an empty string. It does not raise an error.
The rule to remember is start >= end produces ''. Equal positions select no range, and a start position after the end position does not make Python automatically count backward.
Reading the Index Range
Use the source string banana to visualize the positions involved. Its character indices run from 0 through 5, while index 6 is one position past the last character. A slice uses a starting position and an ending position. Characters are taken from the start position up to, but not including, the end position.
For fruit[3:3], the slice begins at index 3 and stops before index 3. There is no space between those two boundaries, so no character can be selected. For fruit[5:2], the proposed range points from a later position to an earlier position. A normal forward slice does not reverse itself, so it also selects nothing. In both cases, the result is the empty string.
Tracing Equal and Reversed Bounds
Three slice checks
Predict the result of each slice using the string banana: fruit[3:3], fruit[5:2], and a slice whose start and end are both beyond the string's length.
- 1Equal bounds
fruit[3:3] starts and ends at the same position. The selected range has zero width, so the result is ''.
- 2Start after end
fruit[5:2] starts after its ending position. A normal slice does not count backward, so the result is ''.
- 3Bounds beyond the string
When the start and end are both beyond the string's length and start equals end, the same start >= end rule applies. The result is still ''.
All three slices produce the empty string.
What do you think happens?
What is the result of the following source examples: fruit[3:3], fruit[5:2], and a forward slice from index 5 to 6?
Reveal answer
The first two produce '', and the last produces 'n'.
Equal bounds select no characters, a start after the end does not reverse a normal slice, and the forward range from index 5 to 6 selects the character at index 5.
Empty Strings as Values
An empty string is a valid string containing zero characters. Its length is 0. It is not an error, and it is not a string containing a space.
Python treats an empty string like any other string, except that it has no characters to process. You can concatenate it, compare it, pass it to functions, or iterate over it. Iterating over an empty string performs no iterations because there are no characters. This uniform behavior means an empty slice does not require a special kind of value.
| Result | Length | Contains a character? |
|---|---|---|
| Empty string | 0 | No |
| String containing a space | Not specified by the source | Yes |
| Slice error | Not applicable | Not a string result |
An empty string is a valid zero-character string, not an error and not a string containing a space.
Backward Slicing
A frequent misunderstanding is to treat a slice with a larger start index as a request to move backward through the string. For example, fruit[5:2] does not return reversed characters. It returns the empty string because the start comes after the end in a normal slice.
If the intention is reversal, use a negative step rather than assuming that placing the larger index first will reverse the characters. The source identifies a negative step as the way to request reversal.
Common mistake Assuming start equal to end returns one character.
What it looks likefruit[3:3]Why it failsThe end position is excluded, and both boundaries are the same. There is no range between them.
Do this insteadTreat the result as an empty string with length 0.
Common mistake Assuming start greater than end automatically reverses the selected characters.
What it looks likefruit[5:2]Why it failsA normal slice does not count backward when its start is after its end.
Do this insteadExpect an empty string. Use a negative step when reversal is intended.
Common mistake Treating an empty slice as an error.
What it looks likeA slice with unexpected or equal bounds produces no visible characters.
Why it failsPython defines an empty string as the valid result for start >= end.
Do this insteadInspect the result as a string value and check its length or the index relationship.
Common mistake Expecting indices beyond the string's length to change the start >= end rule.
What it looks likeA slice whose start and end are equal and both beyond the string.
Why it failsThe same comparison between start and end still determines that no range is selected.
Do this insteadApply the rule start >= end before focusing on whether the positions are beyond the string.
Debugging Empty Results
- 1Write down the slice's start index and end index.
- 2Compare the two values.
- 3If start equals end, recognize that the slice has zero width and produces ''.
- 4If start is greater than end, recognize that a normal slice does not count backward and produces ''.
- 5If start is less than end, check which characters lie in the forward range.
- 6If reversal was intended, check whether a negative step was used.
This process separates three different situations: a valid empty result, a non-empty forward slice, and an intended reversal that needs a negative step. The key diagnostic question is not simply whether the output looks blank. It is whether the slice boundaries describe a range containing any characters.
Check Your Prediction
Using the source string banana, predict the result of each slice: fruit[3:3], fruit[5:2], fruit[0:3], a slice with equal start and end positions beyond the string, and fruit[5:6].
Hints
- Apply start >= end before considering the characters.
- A forward slice includes positions from start up to, but not including, end.
- For fruit[5:6], inspect the character at index 5.
Answer check
Check the five predicted results for the banana slices.
- 1First slice
fruit[3:3] has equal bounds, so the result is ''.
- 2Second slice
fruit[5:2] has start greater than end, so the result is ''.
- 3Third slice
fruit[0:3] is a forward range and produces 'pyt' according to the source's expected answer set.
- 4Fourth slice
Equal start and end positions beyond the string still produce ''.
- 5Fifth slice
fruit[5:6] is a forward range from index 5 to index 6 and produces 'n' according to the source's expected answer set.
The expected answer set is '', '', 'pyt', '', 'n'.
Key Takeaways
- 1A slice with start equal to end selects no characters and produces the empty string.
- 2A slice with start greater than end also produces the empty string in a normal forward slice; it does not reverse the string.
- 3An empty string is a valid string with length 0 and no characters, not an error and not a space.
- 4Indices beyond the string's length do not change the start >= end rule.
- 5When a slice looks unexpectedly empty, compare its start and end indices and check whether a negative step was intended.
At a glance
- A slice produces an empty string whenever its start index is greater than or equal to its end index.
- Equal bounds create a zero-width slice, while reversed bounds do not automatically produce reversed characters.
- Empty strings have length 0, contain no characters, and still behave like strings.
- Debugging an empty slice begins by comparing the start and end indices.
- Use a negative step when the intended operation is reversal.