Concepts / Iterating Through Strings with Loops

Iterating Through Strings with Loops

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

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

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.

next positionnext positionnext position0C1O2D3E
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?

Extracting One Character

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

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

Place the index in brackets: Use the string name followed by [1] to select the character at index 1.

Read the selected character: The expression selects O from CODE.

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.

next characternext characternext characterCindex 0Oindex 1Dindex 2Eindex 3
What character does the loop access next as it moves through the string one position at a time?

word = "CODE" for character in word: print(character)

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.

correct first-character indexword[1]returns Oword[0]returns C
What is the difference between using string[0] and string[1], and which character does each expression return?
  • Using index 1 when you want the first character

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

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

  • Using a non-integer index

    The index inside the brackets must be an integer.

    Fix: Use an integer index such as word[1].

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.

Practice Solution

Trace the generated string LOOP using zero-based indices.

Index 0: The character at index 0 is L.

Index 1: The character at index 1 is O.

Index 3: The character at index 3 is P.

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

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

Key Takeaways

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

Key Takeaways

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