Concepts / Negative Indexing in Strings

Negative Indexing in Strings

A string is a sequence of individual characters that can be accessed one at a time using the bracket operator.

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

One Character at a Time

A string is a sequence of individual characters. Python lets you access one character at a time by placing an integer index inside brackets after the string name. The main idea is to connect each index with the character occupying that position in the sequence.

Ccharacterocharacterdcharacterecharacternextnextnext
What characters are contained in the string, and how are they arranged so each can be accessed separately?

Positions from the Start

Python uses zero-based indexing. That means the first character has index 0, the next character has index 1, and later characters continue with increasing index values. An index represents an offset from the start of the string, so the first character is zero positions away from the start.

0C1o2d3enext indexnext indexnext index
Which character does each index position refer to when the first character is at index 0 rather than index 1?
Worked example

Finding the First Character

Suppose the string is Code. Which index selects its first character?

  1. 1
    Locate the start

    The first character is at the beginning of the sequence, so its zero-based index is 0.

  2. 2
    Apply the bracket operator

    Use the string name followed by brackets containing the integer index 0. This selects the character C.

  3. 3
    Avoid index 1

    Index 1 refers to the next character, o, not the first character.

Result

The first character is selected with index 0, and index 1 selects the second character.

Positions from the End

Negative indexing provides another way to identify characters: count backward from the end of the string. The last character is at index -1, the character immediately before it is at index -2, and the count continues toward the beginning. Positive and negative indices can therefore point to the same character from opposite directions.

0C-4C1o-3o2d-2d3e-1esame charactersame charactersame charactersame character
How do positive indices starting at 0 and negative indices starting at -1 point to the same characters from opposite ends of the string?
Worked example

Selecting the Last Character

Suppose the string is Code. Which index selects its last character?

  1. 1
    Start at the end

    The last character is identified by beginning the backward count at -1.

  2. 2
    Match the character

    For Code, index -1 selects e.

  3. 3
    Compare directions

    The same character is reached from the start with positive index 3 and from the end with negative index -1.

Result

The last character can be selected with negative index -1.

Bracket Selection

The bracket operator uses the form string_name[index]. The index must be an integer. Replace string_name with the name of the string and index with the position you want to select. A non-integer index, such as a float, raises a TypeError.

wordstring name[opens selectionintegercharacter position]closes selectionfollowed bycontainsfollowed by
How does an integer inside brackets select one specific character from a string?

Fixing the First-Character Error

The most common indexing mistake is assuming that the first character is at index 1. In Python, index 1 means one position after the start, so it identifies the second character. To select the first character, use index 0.

word[1]incorrect selectionword[0]correct selectionosecond characterCfirst characterselectsselects
What changes in the selected character when the first character is incorrectly treated as index 1 instead of index 0?
  • Common mistake

    Treating index 1 as the first character

    What it looks like

    For the string Code, using index 1 selects o.

    Why it fails

    Python starts counting at index 0, so index 1 is the second position.

    Do this instead

    Use index 0 when selecting the first character.

  • Common mistake

    Using a non-integer index

    What it looks like

    Trying to use a float as the index.

    Why it fails

    The bracket operator requires the index to be an integer.

    Do this instead

    Use an integer index, such as 0, 1, -1, or -2.

  • Common mistake

    Forgetting which end a negative index starts from

    What it looks like

    Treating -1 as the first character.

    Why it fails

    Negative indexing begins at the end, where -1 identifies the last character.

    Do this instead

    Count backward from the end for negative indices.

Check Your Index

What do you think happens?

For the string Code, which character is selected by index -2?

  • C

  • o

  • d

  • e

Reveal answer
Answer:

d

Negative indices count backward from the end. Index -1 selects e, so index -2 selects the character immediately before it, d.

EASY

For the string Learn, identify the character selected by positive index 0, positive index 1, negative index -1, and negative index -2. Then explain why positive index 1 is not the first character.

Hints
  • Write the characters in order and place positive indices from left to right.
  • For negative indices, begin at the final character and count backward.
  • Remember that the first positive index is 0.

Key Takeaways

  • A string is a sequence of individual characters that can be accessed one at a time.
  • Python uses zero-based indexing, so the first character is at index 0 and the second character is at index 1.
  • The bracket operator has the form string_name[index], and the index must be an integer.
  • Positive indices count from the beginning, while negative indices count backward from the end, with -1 identifying the last character.
  • When selecting the first character, replace the common but incorrect index 1 with index 0.

Related concepts

More concepts starting with N