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.

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

Finding the Delimiter

Definition

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.

18:19space20021.228234247255next characternext character
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.

Code walkthrough
text = "X-DSPAM-Confidence: 0.8475"
value_text = text[20:]
  1. 1
    Store the structured text in text.
  2. 2
    Use text[20:] to extract the characters beginning at index 20.
  3. 3
    The 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.
Output
0.8475
X-DSPAM-Confidence:0.8475complete string0.8475text[20:]slice from index 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
Notes on the code
  • value_text is the extracted numeric text.
  • float(value_text) converts that text into a floating-point value.
Before
value_text = "0.8475"

The extracted value is text.

After
value = float(value_text)

The converted value is a floating-point number.

The Complete Parsing Sequence

Worked example

Extracting Confidence from a Header

Parse the numeric value from the structured string X-DSPAM-Confidence: 0.8475.

  1. 1
    Locate the delimiter

    Call text.find(":"). The colon is at index 18.

  2. 2
    Move to the value

    The space is at index 19, so the numeric value begins at index 20.

  3. 3
    Slice the value text

    Use text[20:] to obtain the substring 0.8475.

  4. 4
    Convert the substring

    Pass the extracted substring to float() so it becomes a floating-point value.

Result

The three-step pattern is find the delimiter, slice from the value boundary, and convert the extracted text with float().

Code walkthrough
text = "X-DSPAM-Confidence: 0.8475"
colon_position = text.find(":")
value_text = text[colon_position + 2:]
confidence = float(value_text)
  1. 1
    find(":") locates the colon and returns 18.
  2. 2
    Adding 2 moves past both the colon and the following space, producing the start position 20.
  3. 3
    The slice from that position to the end produces 0.8475 as text.
  4. 4
    float() converts the extracted text into a value usable in calculations.
  • colon_position stores the delimiter index.
  • The slice begins two positions after the colon because the source string has a space immediately after the colon.
  • value_text contains the numeric substring.
  • float(value_text) converts the substring into a floating-point value.
Output
confidence contains the floating-point value 0.8475
X-DSPAM-Confidence:0.8475structured string18colon index0.8475substring0.8475floating-point valuefind(":")slice after delimiterfloat()
How do find(), slicing, and float() work together when parsing a structured header?

Mistakes at the Boundary

  • Common mistake

    Using the delimiter position as the first position of the value

    What it looks like

    The colon is at index 18, but the value does not begin at index 18.

    Why it fails

    Index 18 contains the colon, so a slice beginning there includes a character that is not part of the numeric value.

    Do this instead

    Start 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 like
    value_text = text[20:]
    Why it fails

    The slice produces the string 0.8475, which is still text.

    Do this instead

    Pass 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 like

    A fixed starting position may not match a different structured string.

    Why it fails

    The purpose of find() is to locate the delimiter and provide the position needed for the slice.

    Do this instead

    Use find() first, then base the slice on the returned index.

Best practice

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.

More concepts starting with S