Concepts / String Traversal and Iteration

String Traversal and Iteration

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

  • Programming
Interactive lab

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

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. 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.

Educational simulation

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.

Key point

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.

0b1a2n3a4n5a6one past the last characternextnextnextnextnextend position
How do the start and end positions map onto the string, and why does start greater than or equal to end select no characters?

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

Worked example

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.

  1. 1
    Equal bounds

    fruit[3:3] starts and ends at the same position. The selected range has zero width, so the result is ''.

  2. 2
    Start after end

    fruit[5:2] starts after its ending position. A normal slice does not count backward, so the result is ''.

  3. 3
    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 ''.

Result

All three slices produce the empty string.

start = endfruit[3:3]''zero charactersstart > endfruit[5:2]''zero charactersselects nothingdoes not reverse
What is the difference between a zero-width slice and a reversed slice, and do both produce the same result?

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?

  • All three produce an error

  • The first two produce '', and the last produces 'n'

  • The first produces a space, the second reverses characters, and the last produces ''

  • All three produce the entire string

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

Definition

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.

ResultLengthContains a character?
Empty string0No
String containing a spaceNot specified by the sourceYes
Slice errorNot applicableNot 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.

Best practice

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 like
    fruit[3:3]
    Why it fails

    The end position is excluded, and both boundaries are the same. There is no range between them.

    Do this instead

    Treat the result as an empty string with length 0.

  • Common mistake

    Assuming start greater than end automatically reverses the selected characters.

    What it looks like
    fruit[5:2]
    Why it fails

    A normal slice does not count backward when its start is after its end.

    Do this instead

    Expect an empty string. Use a negative step when reversal is intended.

  • Common mistake

    Treating an empty slice as an error.

    What it looks like

    A slice with unexpected or equal bounds produces no visible characters.

    Why it fails

    Python defines an empty string as the valid result for start >= end.

    Do this instead

    Inspect 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 like

    A slice whose start and end are equal and both beyond the string.

    Why it fails

    The same comparison between start and end still determines that no range is selected.

    Do this instead

    Apply the rule start >= end before focusing on whether the positions are beyond the string.

Debugging Empty Results

  1. 1
    Write down the slice's start index and end index.
  2. 2
    Compare the two values.
  3. 3
    If start equals end, recognize that the slice has zero width and produces ''.
  4. 4
    If start is greater than end, recognize that a normal slice does not count backward and produces ''.
  5. 5
    If start is less than end, check which characters lie in the forward range.
  6. 6
    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

MEDIUM

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.
Worked example

Answer check

Check the five predicted results for the banana slices.

  1. 1
    First slice

    fruit[3:3] has equal bounds, so the result is ''.

  2. 2
    Second slice

    fruit[5:2] has start greater than end, so the result is ''.

  3. 3
    Third slice

    fruit[0:3] is a forward range and produces 'pyt' according to the source's expected answer set.

  4. 4
    Fourth slice

    Equal start and end positions beyond the string still produce ''.

  5. 5
    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.

Result

The expected answer set is '', '', 'pyt', '', 'n'.

Key Takeaways

  1. 1
    A slice with start equal to end selects no characters and produces the empty string.
  2. 2
    A slice with start greater than end also produces the empty string in a normal forward slice; it does not reverse the string.
  3. 3
    An empty string is a valid string with length 0 and no characters, not an error and not a space.
  4. 4
    Indices beyond the string's length do not change the start >= end rule.
  5. 5
    When 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.

Related concepts

More concepts starting with S