Concepts / Slicing Strings

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

A String as a Sequence

A string is not a single indivisible item. It is a sequence of individual characters. Python lets you access those characters one at a time by placing an index inside brackets after the string.

For example, imagine the string "CODE". Its characters can be considered separately: C, O, D, and E. To choose one character, you identify its integer index and use the bracket operator with that index.

Positions Begin at Zero

Python uses zero-based indexing. The first character is at index 0, not index 1. An index represents an offset from the start of the string: the first character has an offset of zero because no characters come before it. The next character has an offset of one.

0C1O2D3Enext positionnext positionnext position
Which character does each integer index point to, and why is the first character at index 0 rather than index 1?
Key point

When you count characters for human communication, you may naturally say first, second, and third. When you use Python indices, translate those positions to 0, 1, and 2.

Choosing One Character

Definition

The bracket operator uses the form string_name[index], where index must be an integer. Python uses the integer to select one character from the string.

wordstring[opens index2integer]selects characterfollowed bycontainsclosed by
What character does Python return when an integer index is placed inside brackets after a string?
Worked example

Selecting a Character from CODE

Find the character selected by the expression "CODE"[2].

  1. 1
    Step 1: Map the indices

    The characters have zero-based indices: C is at 0, O is at 1, D is at 2, and E is at 3.

  2. 2
    Step 2: Read the requested index

    The bracket contains the integer 2, so the third character is selected.

  3. 3
    Step 3: Identify the character

    The character at index 2 is D.

Result

"CODE"[2] selects D.

Fixing the First-Character Error

A common indexing mistake is to assume that the first character is at index 1. In Python, index 1 identifies the second character because one character comes before it at index 0.

"DATA"[1]selects A"DATA"[0]selects DAsecond characterDfirst characterreturnsreturns
What changes when index 1 is used as the first-character position, and which index correctly retrieves that character?
  • Common mistake

    Using index 1 when you intend to select the first character.

    What it looks like

    For the string "DATA", treating index 1 as the first position selects A instead of D.

    Why it fails

    Python starts string indices at 0, so index 1 is the second character.

    Do this instead

    Use index 0 for the first character.

  • Common mistake

    Using a non-integer as an index.

    What it looks like

    Trying to use a float as the index.

    Why it fails

    The string index must be an integer.

    Do this instead

    Replace the non-integer index with an integer.

Practice the Mapping

EASY

For the string "PYTHON", identify the character at index 0, the character at index 1, and the character at index 4. Then explain why index 1 does not refer to the first character.

Hints
  • Begin by writing the indices from 0 upward.
  • The first character receives index 0.
  • Move one position to the right for each increase in the index.
Worked example

Checking the Practice Mapping

Map the requested indices in "PYTHON" to their characters.

  1. 1
    Index 0

    The first character is P.

  2. 2
    Index 1

    The second character is Y.

  3. 3
    Index 4

    Counting from zero gives P at 0, Y at 1, T at 2, H at 3, and O at 4.

Result

Index 0 selects P, index 1 selects Y, and index 4 selects O. Index 1 is not the first character because the first character is assigned index 0.

Key Takeaways

  1. 1
    A string is a sequence of individual characters.
  2. 2
    Characters can be accessed one at a time with the bracket operator.
  3. 3
    The form string_name[index] uses an integer index to select a character.
  4. 4
    Python uses zero-based indexing, so the first character is at index 0 and the second character is at index 1.
  5. 5
    A non-integer index, such as a float, raises a TypeError.

At a glance

  • A string is a sequence of individual characters that can be accessed separately.
  • Use the bracket operator with an integer index to select a character.
  • Python counts string positions from zero, not one.
  • Index 0 identifies the first character; index 1 identifies the second.
  • Using a non-integer as an index raises a TypeError.

Related concepts

More concepts starting with S