String Traversal and Iteration
A slice with start >= end always produces an empty string, not an error.
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.
Equal bounds: fruit[3:3] starts and ends at the same position. The selected range has zero width, so the result is ''.
Start after end: fruit[5:2] starts after its ending position. A normal slice does not count backward, so the result is ''.
Bounds 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
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 |
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.
Assuming start equal to end returns one character.
The end position is excluded, and both boundaries are the same. There is no range between them.
Fix:
Treat the result as an empty string with length 0.Assuming start greater than end automatically reverses the selected characters.
A normal slice does not count backward when its start is after its end.
Fix:
Expect an empty string. Use a negative step when reversal is intended.Treating an empty slice as an error.
Python defines an empty string as the valid result for start >= end.
Fix:
Inspect the result as a string value and check its length or the index relationship.Expecting indices beyond the string's length to change the start >= end rule.
The same comparison between start and end still determines that no range is selected.
Fix:
Apply the rule start >= end before focusing on whether the positions are beyond the string.
Debugging Empty Results
- Write down the slice's start index and end index.
- Compare the two values.
- If start equals end, recognize that the slice has zero width and produces ''.
- If start is greater than end, recognize that a normal slice does not count backward and produces ''.
- If start is less than end, check which characters lie in the forward range.
- If 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.
First slice: fruit[3:3] has equal bounds, so the result is ''.
Second slice: fruit[5:2] has start greater than end, so the result is ''.
Third slice: fruit[0:3] is a forward range and produces 'pyt' according to the source's expected answer set.
Fourth slice: Equal start and end positions beyond the string still produce ''.
Fifth 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
- A slice with start equal to end selects no characters and produces the empty string.
- A slice with start greater than end also produces the empty string in a normal forward slice; it does not reverse the string.
- An empty string is a valid string with length 0 and no characters, not an error and not a space.
- Indices beyond the string's length do not change the start >= end rule.
- When a slice looks unexpectedly empty, compare its start and end indices and check whether a negative step was intended.
Key Takeaways
- 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.