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

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.

Selecting Positions 1 Through 3

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

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

Move toward the stop: The positions after 1 and before 4 are 2 and 3, containing T and H.

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

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.

[1:4]stop indexPYTHONYTHIndex 4OIndex 4O excluded
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.

omit startomit stopPYTHONfull string[:3]PYT[3:]HON
How does omitting the start or stop index change the selected part of the string?

Using an Omitted Boundary

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

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

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

[: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.

applyselect indices 1 through 4PROGRAM[1:5]ROGR
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

  • Including the stop index in the result

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

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

  • Starting one position after the stated start index

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

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

  • Forgetting what an omitted boundary means

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

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

  • Stopping early when the stop index is omitted

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

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

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. Map each character to its integer position.
  2. Read the first number in [n:m] as the inclusive start index.
  3. Read the second number as the exclusive stop index.
  4. Include positions from the start up to, but not including, the stop.
  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.