Concepts / Understanding String Indexing

Understanding String Indexing

Using a string's length as an index causes an IndexError because valid indices range from 0 to length - 1, not 0 to length.

  • 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…

The Last Position Problem

String indexing lets you access one character by referring to its position. A common mistake is to use the string's length as though it were the position of the final character. The largest valid index is one less than the length, so using the length itself causes an IndexError.

What do you think happens?

Suppose a string has length 5. What is the largest valid index?

  • 4
  • 5
  • The length is always a valid index
Reveal answer

Answer: 4

The valid indices run from 0 through length - 1. For a length of 5, the final valid index is 4. Index 5 is outside the string and causes an IndexError.

Positions Inside a String

Consider the generated example string apple. It has five characters. Counting from the beginning, the first character is at index 0, and the final character is at index 4. The position count starts at zero, which is why the final index is not equal to the length.

same charactersame charactersame charactersame charactersame character0a-5a1p-4p2p-3p3l-2l4e-1e
How do positive indices from the beginning correspond to negative indices counted from the end?

Positive indices count from the beginning: 0 identifies the first character, 1 the second, and so on. Negative indices count from the end: -1 identifies the last character, -2 the second-to-last, and so on. The two indexing systems can refer to the same character. In the example, index 4 and index -1 both identify the final character, e.

The Valid Index Boundary

A string with length L has valid positive indices from 0 through L - 1. There are L positions, but the first position is numbered 0. Therefore, the index equal to L would refer to a position immediately after the final character, not to a character in the string.

one position beyond the endIndex 4eIndex 5IndexError
Why is the largest valid index length - 1, and what happens when length is used as the index?

Finding the Final Position

The generated example string apple has length 5. Which index identifies its final character?

Count the characters: The string contains 5 characters, so its length is 5.

Subtract one: The final positive index is length - 1, which is 5 - 1.

Locate the character: The final positive index is 4, and that position contains e.

The final character is retrieved with fruit[len(fruit) - 1] when fruit represents the string being indexed.

Two Ways to Reach the End

The last character can be reached with a positive index calculated from the length: fruit[len(fruit) - 1]. The expression obtains the string's length and subtracts one to produce the final valid positive index. A shorter alternative is fruit[-1]. The negative index -1 directly means the last character.

ExpressionIndexing directionCharacter selected
fruit[len(fruit) - 1]From the beginning after calculating the final positionLast character
fruit[-1]From the endLast character
fruit[-2]From the endSecond-to-last character

Common Indexing Mistakes

  • Using the string's length as the final index

    The valid positive indices end at length - 1, which is 4 for a string of length 5. Index 5 is outside the string and causes an IndexError.

    Fix: Use length - 1 for the final positive index, or use the negative index -1.

  • Treating -1 as an invalid index because it is negative

    Negative indices are used to count from the end of a string.

    Fix: Use fruit[-1] for the last character and fruit[-2] for the second-to-last character.

  • Confusing the first and last positive indices

    Positive indexing begins at 0, so the first character is at index 0.

    Fix: Count positive positions from 0 and remember that the final position is length - 1.

Check Your Index Reasoning

EASY

A generated string has length 7. State its largest valid positive index, identify the negative index for its last character, and explain why using index 7 would fail.

Hints
  • The first positive index is 0.
  • The largest valid positive index is one less than the length.
  • The negative index -1 identifies the last character.

Practice Answer

A string has length 7. Determine its final positive index and the negative index for its final character.

Find the positive boundary: Subtract one from the length: 7 - 1 gives 6.

Find the negative form: The final character is always identified from the end with -1.

Explain index 7: Index 7 equals the length, so it is one position beyond the final valid index and causes an IndexError.

The largest valid positive index is 6, the negative index for the last character is -1, and index 7 is invalid.

Key Takeaways

  1. A string with length L has valid positive indices from 0 through L - 1.
  2. Using the length itself as an index goes beyond the final character and causes an IndexError.
  3. The final character can be accessed with fruit[len(fruit) - 1].
  4. The negative index -1 accesses the final character, while -2 accesses the second-to-last character.
  5. Positive and negative indices can refer to the same character from opposite counting directions.

Key Takeaways

  • Valid positive string indices begin at 0 and end at length - 1.
  • The string's length is not a valid final index because it is one position beyond the end.
  • Use fruit[len(fruit) - 1] to reach the last character through a positive index.
  • Use fruit[-1] to reach the last character directly with negative indexing.
  • Negative indices count backward from the end and correspond to positive indices counting forward from the beginning.