Concepts / String Indexing and Negative Indices

String Indexing and Negative Indices

A slice with start >= end always produces an empty string, not an error.

  • Programming

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?

  • An error
  • The character at index 3
  • An empty string
  • The characters in reverse order
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.

positionpositionpositionpositionpositionpositionboundarybananastringindex 0character positionindex 1character positionindex 2character positionindex 3character positionindex 4character positionindex 5character positionindex 6one past the last character
Which positions are available when the banana examples use indices from 0 through 6?

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.

reachesproducescomes afterproducesstart 3fruit[3:3]empty stringno characters selectedend 3same boundaryempty stringno backward traversalstart 5fruit[5:2]end 2earlier boundary
How does Python move from the start boundary toward the end boundary, and which range exists to select?

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.

can be used insupportscan be used inempty stringlength 0; no charactersconcatenationsupportedstring withcharacterslength greater than 0iterationno characters to process
What distinguishes an empty slice result from a string that contains a character?

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.

producesenablesfruit[5:2]ordinary sliceempty stringstart greater than endnegative stepbackward directionbackward sliceused for reversal
What changes when the intended direction is backward rather than the ordinary forward slice direction?

Debugging Empty Results

inspectif equalif start is greateraskif backward movement is intendedempty stringslice resultcompare boundariesstart and endstart equals endno rangecheck intendeddirectionforward or backwarduse negative stepwhen reversal is intendedstart exceeds endordinary slice does notreverse
How can checking the start and end indices reveal why a slice returned an empty string?
  • 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

MEDIUM

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 situationResultReason
start equals endempty stringThere is no range between the boundaries.
start exceeds endempty stringThe ordinary slice does not count backward.
forward rangepytThe range contains characters.
equal boundaries beyond the stringempty stringThe start and end still match, so the selected range is empty.
forward range from index 5 to 6nThe forward range contains one character.

Expected results from the source examples

Key Takeaways

  1. A slice with start equal to end produces an empty string.
  2. A slice with start greater than end also produces an empty string in the ordinary slice form.
  3. An empty string has length 0 and contains no characters, but it remains a valid string.
  4. An ordinary backward-looking slice such as fruit[5:2] does not reverse characters; a negative step is needed for reversal.
  5. 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.