Concepts / Iterating Through Strings with Loops

Iterating Through Strings with Loops

The first position in a Python string has index 0, not index 1.

  • Programming

One Character at a Time

A string is a sequence of individual characters. Because the characters are ordered, you can access one character at a time by referring to its position in the string. When you work through those positions in order, you are iterating through the string.

Key point

The first position in a Python string has index 0, not index 1.

Mapping Positions to Indices

Consider the generated example string CODE. Its characters appear in order, and each character is associated with an integer index. The index is an offset from the start of the string: the first character is zero positions away from the start, the second character is one position away, and so on.

0C1O2D3Enext positionnext positionnext position
How do character positions map to integer indices when the first character is at index 0 rather than index 1?
IndexCharacter
0C
1O
2D
3E

The generated string CODE mapped from zero-based indices to characters.

Selecting a Character

The bracket operator selects a character from a string. The general form is string_name[index]. The value inside the brackets must be an integer, such as 0, 1, or 2. The integer identifies the character's index.

wordstring name[opens index expression2integer index]closes index expression
How does an integer index inside brackets select one specific character from a string?
Worked example

Extracting One Character

Use an index to select the character O from the generated string CODE.

  1. 1
    Identify the index

    In the zero-based mapping, O is associated with index 1.

  2. 2
    Place the index in brackets

    Use the string name followed by [1] to select the character at index 1.

  3. 3
    Read the selected character

    The expression selects O from CODE.

Result

The character at index 1 is O.

python
Output
C
D

Following the Loop

A loop can move through a string one character at a time. In the generated trace below, the loop begins with the character at index 0, then proceeds to the next character at index 1, and continues in order. Each step gives the loop one character from the string sequence.

Cindex 0Oindex 1Dindex 2Eindex 3next characternext characternext character
What character does the loop access next as it moves through the string one position at a time?
Code walkthrough
word = "CODE"
for character in word:
    print(character)
  1. 1
    The first iteration uses C, the first character in the sequence.
  2. 2
    The next iteration uses O.
  3. 3
    The next iteration uses D.
  4. 4
    The final iteration uses E.
  • word stores the generated string CODE.
  • The loop takes one character from the string for each iteration.
  • print displays the current character.
Output
C
O
D
E
Key point

Indexing and looping are related but different actions: bracket notation selects a character by a specific integer index, while a loop can visit the characters in sequence.

Fixing the First-Character Error

The most common indexing mistake is treating index 1 as the first character. Python counts the first character at index 0 because an index represents an offset from the beginning. Therefore, string[0] selects the first character, while string[1] selects the next character.

word[1]returns Oword[0]returns Ccorrect first-character index
What is the difference between using string[0] and string[1], and which character does each expression return?
  • Common mistake

    Using index 1 when you want the first character

    What it looks like
    word = "CODE" followed by word[1]
    Why it fails

    Index 1 identifies the second character, O, because the first character is at index 0.

    Do this instead

    Use word[0] when you want the first character, C.

  • Common mistake

    Using a non-integer index

    What it looks like
    word[1.0]
    Why it fails

    The index inside the brackets must be an integer.

    Do this instead

    Use an integer index such as word[1].

Before
word = "CODE"
print(word[1])

The expression selects O, the second character.

After
word = "CODE"
print(word[0])

The expression selects C, the first character.

Try the Trace

EASY

For the generated string "LOOP", identify the character at index 0, the character at index 1, and the character at index 3. Then write the characters in the order a loop would visit them.

Hints
  • Start counting at index 0.
  • Match each index with one character in the string.
  • A loop moving through the sequence visits the characters in their string order.
Worked example

Practice Solution

Trace the generated string LOOP using zero-based indices.

  1. 1
    Index 0

    The character at index 0 is L.

  2. 2
    Index 1

    The character at index 1 is O.

  3. 3
    Index 3

    The character at index 3 is P.

  4. 4
    Loop order

    Moving through the sequence from the beginning visits L, then O, then O, then P.

Result

Index 0 is L, index 1 is O, index 3 is P, and the loop order is L, O, O, P.

Key Takeaways

  1. 1
    A string is an ordered sequence of individual characters.
  2. 2
    Python uses zero-based indexing, so the first character is at index 0.
  3. 3
    The bracket operator uses the form string_name[index] to select one character.
  4. 4
    The index must be an integer; a non-integer index raises a TypeError.
  5. 5
    A loop can visit the characters in a string one at a time and in sequence.

At a glance

  • Strings contain individual characters arranged in sequence.
  • The first character has index 0 because indices count offsets from the start.
  • Use an integer inside brackets to extract a particular character.
  • A loop can move through the characters one at a time.
  • When correcting an indexing mistake, replace the assumed first-character index 1 with index 0.

More concepts starting with I