String Indexing and Accessing Characters
The find() method locates the position of a substring and returns its starting index; use find(substring, start_pos) to search from a specific position onward.
A Position Is a Landmark
A string can contain several pieces of information mixed together, such as an email address, a timestamp, or other text. Parsing means breaking that larger string into smaller pieces so that you can isolate the exact data you need. The find() method helps by locating a substring and reporting the position where that substring starts.
Treat the positions returned by find() as landmarks. Once you know where important characters or substrings occur, you can use those positions as boundaries for a slice.
Mapping Characters to Indices
The position returned by find() is the starting index of the substring it found. In a generated example such as the text containing an email address followed by a status word, find("@") might identify the @ character at index 13. That number is not the position of the whole email address; it is specifically the position where the @ substring begins. The distinction matters because a later slice may need to begin one position after that landmark.
The find() method locates the position of a substring and returns its starting index. If the search begins from a specified position, find(substring, start_pos) searches from that position onward.
From Landmarks to Slices
Extracting a Domain Between Two Landmarks
A larger string contains an email address and other information. Use the position of the @ character and the position of the next space to isolate the domain.
Locate the first landmark: find('@') returns 21. This means that the @ symbol begins at index 21.
Search from that landmark: find(' ', atpos) searches for a space beginning at position 21 and returns 31. The first space after the @ is therefore at index 31.
Move past the @ symbol: The domain begins at atpos + 1, which is position 22. Starting at atpos itself would include the @ symbol in the extracted text.
Use the second landmark as the boundary: The slice data[atpos+1:sppos] starts at position 22 and stops before position 31. Because the end of a slice is excluded, the space at index 31 is not included.
The extracted domain is uct.ac.za.
A slice written with a start and end boundary extracts characters from the start position up to, but not including, the end position. This is why the @ landmark is changed to atpos + 1 and the space position can be used directly as the end boundary. The two find() results become the inputs that define the exact portion to extract.
Searching from a Later Position
The two-argument form find(substring, start_pos) changes where the search begins. Instead of looking from the beginning of the string, it searches for the substring from start_pos onward. In the source example, the search for a space begins at the position of the @ symbol, so the result identifies the first space after that landmark rather than an earlier space elsewhere in the string.
Tracing a Parsing Failure
When parsing produces an unexpected result, follow the indices in order. First inspect the values returned by find(). Next check whether the slice starts at the intended character. Finally check whether the end boundary is the position of the delimiter that should be excluded. Printing intermediate values makes it possible to see which landmark or boundary became incorrect.
Using the position of the @ symbol as the slice start
The @ symbol becomes part of the extracted text.
Fix:
Add one to the @ position when the desired data begins immediately after @.Using the wrong space position
The slice may end before the intended domain or data segment.
Fix:
Pass the relevant landmark as the start_pos argument so find() searches from that point onward.Ignoring a failed search
A missing landmark does not provide a valid position for the intended slice.
Fix:
Verify that find() returned a valid index before using the result in slicing.Inspecting only the final extracted text
The error may be in a landmark search rather than in the slice itself.
Fix:
Print intermediate find() results and then inspect the slice boundaries step by step.
For parsing logic, name and inspect each landmark separately before combining them into a slice. This keeps the flow visible: locate the first marker, locate the next marker from the correct starting position, calculate any needed offset, and then extract the range.
Practice the Index Flow
A string contains a marker at one position and a second delimiter later in the string. Describe the four values you would trace to extract the text between them: the first find() result, the second find() result using the first as start_pos, the adjusted slice start if the first marker should be skipped, and the slice end.
Hints
- Treat each find() result as a landmark.
- Decide whether the first landmark itself belongs in the extracted text.
- Remember that the slice end is excluded.
- Use find() to locate the starting index of a substring. Use find(substring, start_pos) when the search must begin at a later landmark. Use slicing to extract characters from a start index up to, but not including, an end index. When debugging, verify every find() result, check for -1, and trace how each index becomes a slice boundary.
Key Takeaways
- find() returns the starting index of the substring it locates.
- The optional start_pos argument makes find() search from a later position onward.
- A slice uses a start boundary and an exclusive end boundary to extract text.
- Parsing combines landmark searches with slicing to isolate data from a larger string.
- Check for -1 and inspect intermediate indices when debugging parsing logic.