Concepts / String Methods for Searching and Replacing

String Methods for Searching and Replacing

A slice extracts a substring using [n:m], where n is the starting index (inclusive) and m is the stopping index (exclusive).

  • 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 Indexed Characters

A string can be treated as a sequence of characters with index positions. String slicing selects part of that sequence and produces a substring. The notation [n:m] identifies a starting index n and a stopping index m.

The key prediction habit is to read a slice in two parts: include the character at the start index, then continue toward the stop index, but stop just before the character at that stop index.

Reading the Index Range

Definition

In [n:m], n is the starting index and is included in the result. The stopping index m is excluded from the result.

0p1y2t3h4o5n
Which characters are selected by the slice [1:4], and why is the character at index 4 excluded?
Worked example

Selecting the Middle Characters

For the string "python", determine which characters are selected by the slice [1:4].

  1. 1
    Locate the start

    Index 1 contains the character y, and the start index is included.

  2. 2
    Move toward the stop

    Continue through indices 2 and 3, which contain t and h.

  3. 3
    Exclude the stop

    Index 4 contains o, but the stop index is excluded.

Result

The selected substring is "ytho".

Why the Stop Is Excluded

The exclusive stop makes the number of selected characters easy to calculate. A slice from index i to index i+n selects n consecutive characters, written as [i:i+n]. The endpoint marks where selection ends rather than adding another character to the result.

0a1b2c3d4e5f
How does [2:5] select three consecutive characters without including the character at index 5?

Omitting an Endpoint

Either endpoint may be omitted. Omitting the start index means the slice begins at the beginning of the string. Omitting the stop index means the slice continues to the end of the string.

NotationMeaningGenerated example
[:m]Start at the beginning and stop before index m"python"[:3] selects "pyt"
[n:]Start at index n and continue to the end"python"[3:] selects "hon"

Omitted start and stop indices

0p1y2t3h4o5n[:3][3:]
What part of the string is selected when the start or stop index is omitted?

From Full String to Substring

planetfull stringlaneindices 1 through 4[1:5]
How does the slice [1:5] transform the full string "planet" into the selected substring?
Worked example

Predicting Several Slices

For the string "planet", determine the selected substring for [1:5], [:3], and [4:].

  1. 1
    [1:5]

    Start at index 1 and include indices 1, 2, 3, and 4. The character at index 5 is the stop boundary.

  2. 2
    [:3]

    The omitted start means begin at the beginning, then stop before index 3.

  3. 3
    [4:]

    Start at index 4 and continue through the end because the stop index is omitted.

Result

[1:5] selects "lane", [:3] selects "pla", and [4:] selects "et".

What do you think happens?

For the string "python", which characters are selected by [0:5]?

  • "pytho"

  • "python"

  • "ython"

  • "pyt"

Reveal answer
Answer:

"pytho"

The slice starts at index 0 and stops before index 5, so it includes indices 0, 1, 2, 3, and 4. The character at index 5 is excluded.

Mistakes with Slice Boundaries

  • Common mistake

    Including the stop index in the result

    What it looks like

    Expecting [0:5] to include the character at index 5

    Why it fails

    The stop index is exclusive, so [0:5] includes indices 0 through 4 only.

    Do this instead

    Treat the stop index as the ending boundary and stop immediately before it.

  • Common mistake

    Counting characters instead of reading index positions

    What it looks like

    Choosing a slice without checking which index contains the first character

    Why it fails

    The notation refers to index positions, and the selected characters depend on both the start and stop positions.

    Do this instead

    Write or inspect the index for each character, include the start, and exclude the stop.

  • Common mistake

    Assuming an omitted endpoint means an empty selection

    What it looks like

    Reading [:m] as if no starting point were provided

    Why it fails

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

    Do this instead
    Interpret [:m] as beginning-to-m and [n:] as n-to-the-end.
Best practice

When predicting a slice, make a short index map first. Mark the start index as included, mark the stop index as excluded, and then read the characters between those boundaries.

Slice Prediction Practice

MEDIUM

For the string "computer", determine the selected characters for [0:4], [3:7], [:2], and [5:]. For each answer, identify the first included index and the excluded stop boundary, if one is present.

Hints
  • The start index is included.
  • The stop index is excluded.
  • An omitted start begins at the beginning, and an omitted stop continues to the end.
Worked example

Checking the Practice Pattern

For the string "computer", determine the selected characters for [3:7].

  1. 1
    Find the boundaries

    The selection begins at index 3 and ends before index 7.

  2. 2
    List the included positions

    The included indices are 3, 4, 5, and 6.

  3. 3
    Read the characters

    Those positions contain p, u, t, and e.

Result

The selected substring is "pute".

What to Remember

  1. 1
    A slice uses [n:m] to extract a substring.
  2. 2
    The start index n is included, while the stop index m is excluded.
  3. 3
    The notation [:m] slices from the beginning to just before m.
  4. 4
    The notation [n:] slices from n through the end.
  5. 5
    The exclusive stop makes [i:i+n] select n consecutive characters.

At a glance

  • String slices extract substrings using a start index and a stop index.
  • The start index is included, but the stop index is excluded.
  • Omitting the start selects from the beginning; omitting the stop selects through the end.
  • To predict a result, list the included index positions before reading their characters.
  • The exclusive stop allows a slice such as [i:i+n] to select n consecutive characters.

Related concepts

More concepts starting with S