Concepts / String Indexing with Integer Positions

String Indexing with Integer Positions

A slice extracts a substring using [n:m], where n is the starting index (inclusive) and m is the stopping index (exclusive).

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

A String as Indexed Characters

A string can be understood as a sequence of characters, with each character associated with an integer position called an index. A slice selects a substring by giving a starting index and a stopping index in the form [n:m]. To predict the result, first map each position to its character, then identify the positions covered by the slice.

0P1Y2T3H4O5N
Which character does each integer index point to in the string?

Reading the Slice Range

In [n:m], n is the starting index and m is the stopping index. The starting index is inclusive, so the character at position n is included. The stopping index is exclusive, so the character at position m is not included. The selected positions are therefore n, n+1, and the positions before m.

Worked example

Selecting Positions 1 Through 3

Given the string PYTHON, determine the characters selected by [1:4].

  1. 1
    Find the start

    The start index is 1, so the character at position 1, Y, is included.

  2. 2
    Move toward the stop

    The positions after 1 and before 4 are 2 and 3, containing T and H.

  3. 3
    Leave out the stop

    Index 4 contains O, but index 4 is the exclusive stopping position, so O is not selected.

Result

The selected substring is YTH.

0P1Y2T3H4O
Which characters are included by [1:4], and where does the range stop?

What do you think happens?

For the string PYTHON, which characters are selected by [0:5]?

  • PYTH

  • PYTHO

  • YTHON

  • PYTHON

Reveal answer
Answer:

PYTHO

The slice begins at index 0 and includes positions 0, 1, 2, 3, and 4. The stop index 5 is excluded, so the character at index 5 is not selected.

Why the Stop Is Excluded

The exclusive stop creates a consistent boundary: start at n and continue up to, but not including, m. This makes the number of selected characters easy to describe. A slice written as [i:i+n] selects n consecutive characters, because the positions from i through the position before i+n contain exactly n positions. The exclusive stop is therefore intentional and helps avoid off-by-one confusion when selecting a known number of characters.

PYTHONYTHIndex 4OIndex 4O excluded[1:4]stop index
What happens to the character at the stop index m?

Omitting a Boundary

A slice does not have to show both boundaries. If the start index is omitted, as in [:m], the slice begins at the beginning of the string and stops before index m. If the stop index is omitted, as in [n:], the slice begins at index n and continues to the end of the string.

NotationStarting pointStopping point
[n:m]Index nBefore index m
[:m]Beginning of the stringBefore index m
[n:]Index nEnd of the string

The omitted boundary supplies the beginning or end of the string.

PYTHONfull string[:3]PYT[3:]HONomit startomit stop
How does omitting the start or stop index change the selected part of the string?
Worked example

Using an Omitted Boundary

Given the string PYTHON, determine the results of [:3] and [3:].

  1. 1
    Read [:3]

    The omitted start means begin at the beginning. The stop index is 3, so positions 0, 1, and 2 are selected.

  2. 2
    Read [3:]

    The start index is 3, and the omitted stop means continue to the end. Positions 3, 4, and 5 are selected.

Result

[:3] selects PYT, and [3:] selects HON.

Predicting Selected Characters

To predict a slice, use a fixed three-step process. First, write the integer position under each character. Second, identify the start position and the position immediately before the stop position. Third, read only those characters in order. For an omitted start, begin with the first character. For an omitted stop, continue through the final character.

PROGRAM[1:5]ROGRapplyselect indices 1 through 4
Given a string and a slice such as [1:5], which exact characters appear in the resulting substring?
MEDIUM

For the string NOTEBOOK, predict the result of [2:6], [:4], and [5:].

Hints
  • Write the positions under N, O, T, E, B, O, O, K.
  • For [2:6], include positions 2, 3, 4, and 5.
  • For [:4], begin at the beginning and stop before position 4.
  • For [5:], begin at position 5 and continue to the end.

Frequent Boundary Mistakes

  • Common mistake

    Including the stop index in the result

    What it looks like

    Treating [0:5] as selecting positions 0, 1, 2, 3, 4, and 5

    Why it fails

    The stop index is exclusive, so position 5 is not part of the result.

    Do this instead

    Select positions 0 through 4 for [0:5].

  • Common mistake

    Starting one position after the stated start index

    What it looks like

    Treating [2:5] as beginning at position 3

    Why it fails

    The starting index is inclusive, so the character at position 2 must be included.

    Do this instead

    Begin at position 2 and continue through the position before 5.

  • Common mistake

    Forgetting what an omitted boundary means

    What it looks like

    Reading [:m] as if it had no starting point

    Why it fails

    An omitted start means the slice begins at the beginning of the string.

    Do this instead

    Interpret [:m] as starting at the beginning and stopping before m.

  • Common mistake

    Stopping early when the stop index is omitted

    What it looks like

    Reading [n:] as if a hidden stop index ended the slice after one character

    Why it fails

    An omitted stop means the slice continues to the end of the string.

    Do this instead

    Include the character at n and every following character through the end.

Best practice

When uncertain, list the positions explicitly. Treat the left boundary as included and the right boundary as excluded. This simple habit makes the selected characters visible and reduces off-by-one confusion.

Slice Selection Checklist

  1. 1
    Map each character to its integer position.
  2. 2
    Read the first number in [n:m] as the inclusive start index.
  3. 3
    Read the second number as the exclusive stop index.
  4. 4
    Include positions from the start up to, but not including, the stop.
  5. 5
    If the start is omitted, begin at the beginning; if the stop is omitted, continue to the end.

Key Takeaways

  • A slice uses [n:m] to extract a substring.
  • The start index n is included, while the stop index m is excluded.
  • Omitting the start selects from the beginning, and omitting the stop selects to the end.
  • The notation [i:i+n] selects n consecutive characters.
  • To predict a result, map positions to characters and read from the inclusive start up to the exclusive stop.

More concepts starting with S