String Methods: split(), strip(), and rstrip()
find() returns the starting index of a substring if found, or -1 if not found
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
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.
Locate the beginning: The substring @example.com begins after the five characters a, l, i, c, and e.
Apply zero-based indexing: Those five preceding characters occupy positions 0 through 4, so the @ character begins at position 5.
Interpret 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)
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.
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
Treating 0 as a failed search
The first character has index 0, so 0 means the substring starts at the beginning.
Fix:
Treat every result other than -1 as a found substring.Forgetting that -1 means the substring is absent
The returned integer is the signal that distinguishes a matching line from a nonmatching line.
Fix:
Compare the result with -1 and skip the line when the result equals -1.Searching a file line before calling rstrip()
A line read from a file includes the newline at the end, so the text being searched still contains trailing whitespace.
Fix:
Call rstrip() before find() when processing file lines.Using the returned index as though it were the end of the substring
find() returns the starting index of the substring.
Fix:
Trace 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.
Result 0: The substring begins at the first character. It is found, so the line passes the not-equal-to-minus-one test.
Result 7: The substring begins at character index 7. It is found, so the line passes the filter.
Result -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
- find() returns the starting index of a found substring.
- String indexes are zero-indexed, so 0 is a valid match at the beginning.
- A return value of -1 means that the substring was not found.
- For file lines, call rstrip() before searching to remove trailing whitespace, including the newline.
- A loop and conditional can use the find() result to print matching lines and skip nonmatching lines.
Key Takeaways
- 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.