File Input and the open() Function
find() returns the starting index of a substring if found, or -1 if not found
A Search Produces a Position
When find() searches a string, it returns one integer. If the substring exists, the integer identifies the character position where that substring starts. If the substring does not exist, find() returns -1. This makes the method useful for examining file lines and deciding which lines should be kept.
The first character has index 0, not index 1. A returned index is the starting position of the complete substring.
What do you think happens?
In the string From: alice@example.com, what does find() return when searching for @?
Reveal answer
Answer: 11
The first character is at index 0. Counting from zero, the @ character is at index 11.
Mapping Characters to Indexes
To debug find(), write the string out with its character positions. In the generated example below, the search substring is @. The returned value is not a count of all characters and not a true-or-false result. It is the position of the first character in the substring.
Tracing an Email Address
Determine the result of searching for @ in the string From: alice@example.com.
Start at index 0: The first character, F, is at position 0, so every later character is counted from that starting point.
Count to the match: The @ character appears after the characters From: and the space and the five letters in alice. Its zero-based position is 11.
Interpret the result: find() returns 11 because the searched substring begins at index 11.
The search returns 11.
Three Return Values
| find() result | Meaning | Typical interpretation |
|---|---|---|
| 0 | The substring starts at the first character. | The line begins with the searched substring. |
| A positive index | The substring starts later in the string. | The line contains the substring after one or more earlier characters. |
| -1 | The substring was not found. | Reject or skip the string when filtering for the substring. |
Filtering Lines from a File
The useful file-search pattern combines three actions: open the file, process each line in a loop, and use find() inside a conditional statement. The line is cleaned with rstrip() before the search. If find() returns -1, continue skips that line. Otherwise, the line is printed.
fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if line.find('From:') == -1: continue print(line)
From: alice@example.com
From: bob@example.comFollowing the Matching Data
Each file line follows the same path: it comes from the opened file, has trailing whitespace removed, and is tested with find(). A line whose result is -1 leaves the output collection because continue skips it. A line whose result is 0 or a positive index passes the test and is printed. The method therefore acts as a filter rather than merely reporting a position.
Treat the return value as diagnostic information. When a line is unexpectedly accepted or rejected, inspect the exact integer from find(), then compare that result with -1. This reveals whether the substring was found and where it began.
Mistakes with File Searches
Treating 0 as if it meant no match.
Zero is a valid index. It means the substring starts at the first character.
Fix:
Use -1 as the not-found result. A result of 0 or a positive index means that the substring was found.Checking only for a positive result.
That condition rejects a valid match at index 0.
Fix:
Test whether the result is -1 when you need to distinguish absence from presence.Searching before removing trailing whitespace.
A line read from a file includes a newline character at the end, and trailing whitespace can make searches behave unexpectedly.
Fix:
Call rstrip() before calling find() so the search uses the line's content without trailing whitespace.Assuming find() returns the number of matching characters.
find() returns the starting index, not the length of the substring.
Fix:
Trace the characters from index 0 and identify where the first character of the searched substring begins.
Practice the Trace
For each string, determine the result of find('cat'). Then decide whether the line would be printed by a filter that skips only when the result equals -1: catalog, bobcat, and dog.
Hints
- Start counting at index 0.
- The substring can begin at index 0 or at a positive index.
- Only a result of -1 causes the line to be skipped.
Checking the Practice Strings
Determine the find('cat') result for catalog, bobcat, and dog.
catalog: The substring cat starts at the first character, so the result is 0. The line passes the filter.
bobcat: The substring cat starts after bob. With zero-based indexing, its starting position is 3. The line passes the filter.
dog: The substring cat is absent, so the result is -1. The line is skipped.
catalog and bobcat are accepted; dog is rejected.
Key Takeaways
- find() returns the starting index of a found substring.
- Indexes are zero-based, so the first character is at position 0.
- A result of 0 is a valid match at the beginning, a positive result is a match later in the string, and -1 means no match.
- For file filtering, open the file, process each line, call rstrip(), and use find() in a conditional statement.
- When debugging, trace the returned index and check whether it equals -1.
Key Takeaways
- find() reports where a substring begins or returns -1 when it is absent.
- The returned position uses zero-based indexing.
- A file-search loop can use rstrip(), find(), and continue to filter lines.
- Checking the exact integer returned by find() is a reliable way to debug substring searches.