String Indexing Fundamentals
Use find() to locate a delimiter like a colon within a string, which returns the index of that character.
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
From Structured Text to Usable Data
Real-world data often arrives as text with a predictable structure. Email headers, configuration files, and log entries can use delimiters such as colons or commas to separate labels from values. To use a value in a calculation, you must locate the delimiter, extract the relevant text, and convert that text to the appropriate type.
The central pattern is find, slice, convert. First, find() locates the delimiter and gives its index. Next, slicing uses the position after the delimiter to isolate the value. Finally, float() converts a numeric string into a floating-point value that Python can use in calculations. Each step depends on the result of the previous step.
Finding the Delimiter
The find() method returns the index of the first occurrence of a substring. When the substring is a delimiter such as a colon, the returned index identifies the boundary between the label and the value.
18For the string X-DSPAM-Confidence: 0.8475, find(":") returns 18. The colon is at index 18, so the returned number tells you exactly where the delimiter is located. That position becomes the reference point for deciding where the value begins.
Slicing from the Value Boundary
Finding the delimiter does not extract the value by itself. You must begin the slice after the delimiter. In the source string, the colon is at index 18, the space after it is at index 19, and the numeric value begins at index 20. Therefore, str[20:] extracts everything from index 20 through the end of the string.
text = "X-DSPAM-Confidence: 0.8475" value_text = text[20:]
Converting Text into a Number
After slicing, the result is still the string 0.8475. It is text representing a number. The float() function takes a string representation of a number and returns the corresponding floating-point value, which Python can use in calculations.
0.8475The Complete Parsing Sequence
Extracting Confidence from a Header
Parse the numeric value from the structured string X-DSPAM-Confidence: 0.8475.
Locate the delimiter: Call text.find(":"). The colon is at index 18.
Move to the value: The space is at index 19, so the numeric value begins at index 20.
Slice the value text: Use text[20:] to obtain the substring 0.8475.
Convert the substring: Pass the extracted substring to float() so it becomes a floating-point value.
The three-step pattern is find the delimiter, slice from the value boundary, and convert the extracted text with float().
text = "X-DSPAM-Confidence: 0.8475" colon_position = text.find(":") value_text = text[colon_position + 2:] confidence = float(value_text)
Mistakes at the Boundary
Using the delimiter position as the first position of the value
Index 18 contains the colon, so a slice beginning there includes a character that is not part of the numeric value.
Fix:
Start the slice after the delimiter. In the source string, also move past the space so the slice begins at index 20.Stopping after slicing and treating the result as a number
The slice produces the string 0.8475, which is still text.
Fix:
Pass the extracted substring to float() when you need a floating-point value for calculations.Skipping find() and guessing the boundary
The purpose of find() is to locate the delimiter and provide the position needed for the slice.
Fix:
Use find() first, then base the slice on the returned index.
Treat the index returned by find() as the link between searching and slicing. Do not view the three operations as unrelated commands: find() identifies the boundary, slicing extracts the portion after that boundary, and float() changes the extracted text into a usable numeric value.
Practice the Pattern
Given the structured string price:12.50, describe the three operations needed to obtain the numeric value 12.50: locate the colon, slice the text after the delimiter, and convert the extracted text with float().
Hints
- Use find(":") to locate the delimiter.
- Begin the slice after the colon.
- The slice result is text until float() converts it.
What do you think happens?
For the source string X-DSPAM-Confidence: 0.8475, what should be passed to float()?
Reveal answer
Answer: The sliced substring 0.8475
find() produces the delimiter index, slicing produces the numeric text, and float() converts that extracted text into a floating-point value.
Key Takeaways
- find() returns the index of the first occurrence of a delimiter such as a colon.
- Use the delimiter position to determine where the value begins, then slice through the end of the string.
- The sliced result is still text, so use float() to convert a numeric string into a floating-point value.
- The reusable parsing pattern is find, slice, convert.
- This pattern applies to structured text such as headers, configuration data, log entries, CSV data, and API responses.