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.
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
- Each method call reads the string it is called on from left to right (strip also trims from the right end).
- 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.
- 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.
- 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.
- 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.
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.
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.
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?
Reveal 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.
Reading a Match Position
Suppose find() searches the string alice@example.com for the substring @example.com.
- 1Locate the beginning
The substring @example.com begins after the five characters a, l, i, c, and e.
- 2Apply zero-based indexing
Those five preceding characters occupy positions 0 through 4, so the @ character begins at position 5.
- 3Interpret the result
find()returns 5 because 5 is the starting index of the requested substring.
A returned index identifies the first character of the matching substring, not the position of its last character.
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.
for line in open('mbox-short.txt'):
line = line.rstrip()
if line.find('From:') == -1:
continue
print(line)- 1The for loop processes the file one line at a time.
- 2
rstrip()is called before the search so trailing whitespace, including the newline character at the end of a file line, is removed. - 3The conditional compares the return value from
find()with -1. - 4When the result is -1, continue moves to the next iteration without printing.
- 5When 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.
From: sender@example.com
From: support@example.comCleaning 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.
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.
Mistakes with Search Results
Common mistake Treating 0 as a failed search
What it looks likeA condition rejects a result of 0 because it assumes the first position means no match.
Why it failsThe first character has index 0, so 0 means the substring starts at the beginning.
Do this insteadTreat every result other than -1 as a found substring.
Common mistake Forgetting that -1 means the substring is absent
What it looks likeA program prints every line without checking whether find() returned -1.
Why it failsThe returned integer is the signal that distinguishes a matching line from a nonmatching line.
Do this insteadCompare 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 likeThe line is searched while it still contains its trailing newline character.
Why it failsA line read from a file includes the newline at the end, so the text being searched still contains trailing whitespace.
Do this insteadCall 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 likeA result of 5 is described as the position where the match ends.
Why it failsfind()returns the starting index of the substring.Do this insteadTrace the index to the first character of the matching substring.
Practice the Trace
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.
Interpreting Three Results
Classify the results 0, 7, and -1 in a find() filter.
- 1Result 0
The substring begins at the first character. It is found, so the line passes the not-equal-to-minus-one test.
- 2Result 7
The substring begins at character index 7. It is found, so the line passes the filter.
- 3Result -1
The substring is absent. The conditional uses continue, so the line is skipped.
The filter prints the lines with results 0 and 7 and skips the line with result -1.
Summary
- 1
find()returns the starting index of a found substring. - 2String indexes are zero-indexed, so 0 is a valid match at the beginning.
- 3A return value of -1 means that the substring was not found.
- 4For file lines, call
rstrip()before searching to remove trailing whitespace, including the newline. - 5A 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.