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:]- 1Store the structured text in text.
- 2Use text[20:] to extract the characters beginning at index 20.
- 3The resulting substring is 0.8475.
- The original string contains the label, delimiter, space, and numeric text.
- The slice starts at index 20, where the numeric value begins.
- The slice continues to the end of the string.
0.8475Converting 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.8475value_textis the extracted numeric text.float(value_text)converts that text into a floating-point value.
value_text = "0.8475"The extracted value is text.
value = float(value_text)The converted value is a floating-point number.
The Complete Parsing Sequence
Extracting Confidence from a Header
Parse the numeric value from the structured string X-DSPAM-Confidence: 0.8475.
- 1Locate the delimiter
Call
text.find(":"). The colon is at index 18. - 2Move to the value
The space is at index 19, so the numeric value begins at index 20.
- 3Slice the value text
Use text[20:] to obtain the substring 0.8475.
- 4Convert 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)- 1
find(":")locates the colon and returns 18. - 2Adding 2 moves past both the colon and the following space, producing the start position 20.
- 3The slice from that position to the end produces 0.8475 as text.
- 4
float()converts the extracted text into a value usable in calculations.
colon_positionstores the delimiter index.- The slice begins two positions after the colon because the source string has a space immediately after the colon.
value_textcontains the numeric substring.float(value_text)converts the substring into a floating-point value.
confidence contains the floating-point value 0.8475Mistakes at the Boundary
Common mistake Using the delimiter position as the first position of the value
What it looks likeThe colon is at index 18, but the value does not begin at index 18.
Why it failsIndex 18 contains the colon, so a slice beginning there includes a character that is not part of the numeric value.
Do this insteadStart the slice after the delimiter. In the source string, also move past the space so the slice begins at index 20.
Common mistake Stopping after slicing and treating the result as a number
What it looks likevalue_text = text[20:]Why it failsThe slice produces the string 0.8475, which is still text.
Do this insteadPass the extracted substring to float() when you need a floating-point value for calculations.
Common mistake Skipping find() and guessing the boundary
What it looks likeA fixed starting position may not match a different structured string.
Why it failsThe purpose of
find()is to locate the delimiter and provide the position needed for the slice.Do this insteadUse 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
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.