Concepts / String Methods: split(), strip(), and rstrip()

String Methods: split(), strip(), and rstrip()

The first character in a string has index 0. Therefore, a result of 0 means that the substring was found at the beginning of the string, not that it was missing.

  • Programming
Interactive lab

Try it: String methods, character by character

How Python's str methods (split, strip, replace, find, count, startswith, join, in …) scan a string character by character — and that each call returns a new value while the original string never changes.

How it works

  1. Each method call reads the string it is called on from left to right (strip also trims from the right end).
  2. split() with no separator skips runs of whitespace and never returns empty strings; split(sep) cuts at every occurrence of sep and can return empty pieces; maxsplit limits the number of cuts.
  3. find, index, count, replace and `in` slide a window along the string; occurrences never overlap, find gives -1 and index raises ValueError when nothing matches.
  4. strip(chars) removes any of the given characters (a set, not a substring) from the ends until it meets one that is not in the set.
  5. Strings are immutable: every call returns a new str, list, int or bool, and the next call in the chain works on that new value.

Default run (62 steps): s = ' Apples, pears,,figs '. Evaluate ' | '.join(s.strip().lower().split(',')) one method call at a time, left to right. … ' | '.join(s.strip().lower().split(',')) → 'apples | pears | | figs'. And s is still ' Apples, pears,,figs '.

Simplified: Printable ASCII text up to 30 characters and up to four chained calls with short arguments. CPython searches with a faster algorithm (fastsearch) than the window-by-window scan shown, but the results are the same.

Educational simulation

Loading the simulation…

A Number That Locates Text

A substring search does not simply answer yes or no. When find() locates the requested substring, it returns a single integer: the starting character index of the match. If the substring is absent, it returns -1. Learning to interpret that integer is the key to using find() correctly.

Key point

The first character in a string has index 0. Therefore, a result of 0 means that the substring was found at the beginning of the string, not that it was missing.

alice@example.comoriginal stringa0@example.comstarts at index 55find() resultl1i2c3e4@5containscontainscontainscontainscontainsmatch begins heremarks startreturns starting index
Which character position does find() return when a substring begins inside a string?

Tracing the Return Value

To debug find(), separate the search into two questions. First ask whether the returned integer is -1. If it is -1, the substring was not found. If it is not -1, the substring was found, and the integer identifies the position where that substring starts. Because indexes begin at 0, the value 0 is a successful match at the first character.

What do you think happens?

A search returns 0. Was the substring missing, or was it found at the beginning?

  • The substring was missing

  • The substring was found at the beginning

  • The search returned an invalid value

Reveal answer
Answer:

The substring was found at the beginning.

Index positions are zero-indexed, so the first character is at position 0. The missing-substring result is -1.

Worked example

Reading a Match Position

Suppose find() searches the string alice@example.com for the substring @example.com.

  1. 1
    Locate the beginning

    The substring @example.com begins after the five characters a, l, i, c, and e.

  2. 2
    Apply zero-based indexing

    Those five preceding characters occupy positions 0 through 4, so the @ character begins at position 5.

  3. 3
    Interpret the result

    find() returns 5 because 5 is the starting index of the requested substring.

Result

A returned index identifies the first character of the matching substring, not the position of its last character.

alice@example.comsearch text: @example.com5substring foundalice@example.comsearch text: @school.org-1substring absentfind()find()
How does the result change when the requested substring is present or absent?

Filtering with a Conditional

The most useful conditional test is whether find() returns -1. A result of -1 means the line does not contain the substring, so the program can skip that line. Any result other than -1 means the substring was found, including a result of 0.

Code walkthrough
for line in open('mbox-short.txt'):
    line = line.rstrip()
    if line.find('From:') == -1:
        continue
    print(line)
  1. 1
    The for loop processes the file one line at a time.
  2. 2
    rstrip() is called before the search so trailing whitespace, including the newline character at the end of a file line, is removed.
  3. 3
    The conditional compares the return value from find() with -1.
  4. 4
    When the result is -1, continue moves to the next iteration without printing.
  5. 5
    When the result is not -1, the line contains the substring and is printed.
  • The loop reads each line from mbox-short.txt.
  • rstrip() removes trailing whitespace from the line.
  • find('From:') returns -1 when the substring is absent.
  • continue skips a line whose search result is -1.
  • print(line) runs for lines in which the substring was found.
Output
From: sender@example.com
From: support@example.com
file lineafter rstrip()find('From:')returns an integerresult == -1substring absent?continuedo not printprint(line)matching linesearchreturns index-1not -1, including 0
How does a find() result control whether a line passes through a conditional filter?

Cleaning Lines Before Searching

When a file is read with a for loop, each line includes the newline character at its end. This character is invisible in ordinary output, but it is still part of the line's text. Calling rstrip() removes trailing whitespace, including that newline, before the search is performed. The result is a search against the line's visible content rather than against content that still includes trailing whitespace.

mbox-short.txtfile inputfile lineincludes newlinerstrip()remove trailing whitespacefind('From:')returns index or -1matching lineprintednonmatching lineskippedread one linecleansearchresult not -1result -1
How does each file line move through cleanup, substring testing, and output?

Method Scope in This Lesson

The section title names split(), strip(), and rstrip(), but the supplied material specifically explains find() and the use of rstrip() on file lines. It does not provide definitions or transformation rules for split() or strip(). This lesson therefore focuses on the documented search pattern: clean a line with rstrip(), call find(), interpret the returned integer, and use that result in a conditional.

split()not defined in suppliedmaterialclean file linebefore searchingstrip()not defined in suppliedmaterialsearch resultstarting index or -1rstrip()removes trailing whitespacefind()returns index or -1preparesproduces
Which methods are described by the supplied material, and what documented role does each have here?

Mistakes with Search Results

  • Common mistake

    Treating 0 as a failed search

    What it looks like

    A condition rejects a result of 0 because it assumes the first position means no match.

    Why it fails

    The first character has index 0, so 0 means the substring starts at the beginning.

    Do this instead

    Treat every result other than -1 as a found substring.

  • Common mistake

    Forgetting that -1 means the substring is absent

    What it looks like

    A program prints every line without checking whether find() returned -1.

    Why it fails

    The returned integer is the signal that distinguishes a matching line from a nonmatching line.

    Do this instead

    Compare the result with -1 and skip the line when the result equals -1.

  • Common mistake

    Searching a file line before calling rstrip()

    What it looks like

    The line is searched while it still contains its trailing newline character.

    Why it fails

    A line read from a file includes the newline at the end, so the text being searched still contains trailing whitespace.

    Do this instead

    Call rstrip() before find() when processing file lines.

  • Common mistake

    Using the returned index as though it were the end of the substring

    What it looks like

    A result of 5 is described as the position where the match ends.

    Why it fails

    find() returns the starting index of the substring.

    Do this instead

    Trace the index to the first character of the matching substring.

Practice the Trace

MEDIUM

A file-processing loop calls rstrip() on each line and then searches for the substring From:. For one line, find() returns 0. For another line, it returns 7. For a third line, it returns -1. Explain what happens to each line in the conditional filter.

Hints
  • Remember that indexes begin at 0.
  • Compare each result with -1.
  • A result of -1 causes continue; any other result allows the line to be printed.
Worked example

Interpreting Three Results

Classify the results 0, 7, and -1 in a find() filter.

  1. 1
    Result 0

    The substring begins at the first character. It is found, so the line passes the not-equal-to-minus-one test.

  2. 2
    Result 7

    The substring begins at character index 7. It is found, so the line passes the filter.

  3. 3
    Result -1

    The substring is absent. The conditional uses continue, so the line is skipped.

Result

The filter prints the lines with results 0 and 7 and skips the line with result -1.

Summary

  1. 1
    find() returns the starting index of a found substring.
  2. 2
    String indexes are zero-indexed, so 0 is a valid match at the beginning.
  3. 3
    A return value of -1 means that the substring was not found.
  4. 4
    For file lines, call rstrip() before searching to remove trailing whitespace, including the newline.
  5. 5
    A loop and conditional can use the find() result to print matching lines and skip nonmatching lines.

At a glance

  • find() reports where a substring starts rather than returning only a yes-or-no answer.
  • The values 0 and other nonnegative indexes indicate a match; -1 indicates no match.
  • rstrip() prepares file lines for searching by removing trailing whitespace and the ending newline.
  • The pattern rstrip(), find(), conditional test, and continue can extract only matching lines from a file.

Related concepts

More concepts starting with S