Concepts / String Indexing Fundamentals

String Indexing Fundamentals

Use find() to locate a delimiter like a colon within a string, which returns the index of that character.

  • Programming
Interactive lab

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

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. 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.

Educational simulation

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.

searchreturnssets startproducesinputreturnsprice:12.50structured textfind(":")locate delimiterdelimiter indexboundary positiontext[index + 1:]extract value text12.50numeric textfloat("12.50")convert type12.50floating-point value
How does structured text move through delimiter lookup, substring extraction, and numeric conversion?

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.

python
Output
18

For 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.

next characternext character18:19space20021.228234247255
What index does find() return, and how does that position identify the boundary between the label and the value?

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:]

slice from index 20X-DSPAM-Confidence:0.8475complete string0.8475text[20:]
Which characters are included when the slice starts at the position after the delimiter and its following space?

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.

python
Output (expected)
0.8475

The 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)

find(":")slice after delimiterfloat()X-DSPAM-Confidence:0.8475structured string18colon index0.8475substring0.8475floating-point value
How do find(), slicing, and float() work together when parsing a structured header?

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

EASY

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()?

  • The complete string
  • The colon
  • The sliced substring 0.8475
  • The index 18
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.