Concepts / String Slicing Syntax and Boundaries

String Slicing Syntax and Boundaries

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 Label to Value

Structured text often places a label and its value on the same line, separated by a delimiter such as a colon. To use the value in a calculation, you need to locate the delimiter, extract the text after it, and convert that text into the appropriate type. This article follows the sequence find, slice, convert.

python

What do you think happens?

What index does line.find(':') return for the string X-DSPAM-Confidence: 0.8475?

Reveal answer

Answer: 18

The find() method returns the index of the first occurrence of the searched substring. In this string, the colon is at index 18.

Locating the Delimiter

The find() method returns the index of the first occurrence of a substring. Calling line.find(':') searches line for a colon and returns the position where that colon appears. For X-DSPAM-Confidence: 0.8475, the result is 18. That index identifies the delimiter, but it is not yet the beginning of the numeric value.

next charactervalue begins after space:index 18spaceindex 190.8475starts at index 20
What index does the delimiter occupy, and how does that position identify where the relevant substring begins?

The delimiter's index is a reference point. In this example, the colon is at index 18, the space after it is at index 19, and the numeric value begins at index 20.

Choosing the Slice Boundary

Once the delimiter has been located, the slice must begin where the desired value begins. The colon is not part of the value, and the space immediately after the colon is not part of the numeric text either. Therefore, line[20:] extracts the characters from index 20 through the end of the string.

slice from index 20X-DSPAM-Confidence:0.8475original string0.8475line[20:]
Which characters are included or excluded when slicing from the position after the delimiter to the end of the string?

line = 'X-DSPAM-Confidence: 0.8475' value_text = line[20:]

Converting Text into a Number

After slicing, the result is the string 0.8475. It is still text, even though it looks like a number. The float() function takes a string representation of a number and returns a floating-point value that Python can use in calculations.

find(':')slice from index 20float()X-DSPAM-Confidence:0.8475structured text18colon index0.8475string substring0.8475float value
How does the data move from the original structured string to the extracted substring and then to a numeric float?

Parsing the Confidence Value

Extract the numeric value from X-DSPAM-Confidence: 0.8475 and convert it to a float.

Find: Call find(':'). The colon is at index 18.

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

Slice: Use line[20:] to extract the substring 0.8475.

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

The structured text has been parsed into the numeric value 0.8475.

python
Output
position is 18
value_text is '0.8475'
confidence is 0.8475

Mistakes at the Boundary

  • Starting the slice at the delimiter index

    Index 18 contains the colon, so the extracted text includes the delimiter instead of beginning with only the value.

    Fix: Start after the delimiter and the following space. For this source string, that is index 20.

  • Starting the slice at the space index

    Index 19 contains the space after the colon, so the extracted substring begins with an unwanted space.

    Fix: Start at index 20 to extract 0.8475.

  • Using the sliced text without conversion

    The result is still a string representation of a number, not a floating-point value.

    Fix: Pass the substring to float() when you need a numeric value for calculations.

Treat the result of find() as the location of the delimiter, then inspect what follows it before choosing the slice start. In the source example, both the delimiter and the space must be skipped. After slicing, convert the result when the program needs a usable number rather than text.

Parsing Practice

EASY

For the string X-DSPAM-Confidence: 0.8475, identify the delimiter index, the first index of the numeric value, the substring produced by slicing from that position to the end, and the result of applying float() to that substring.

Hints
  • Use find(':') to locate the colon.
  • The space follows the colon, and the numeric value begins after that space.
  • The slice should continue to the end of the string.
  1. The reliable sequence is find, slice, convert. find(':') returns the delimiter's index. The slice begins at the first character of the desired value and continues to the end. float() converts the extracted numeric text into a floating-point value.

Key Takeaways

  • find() returns the index of the first occurrence of a substring such as a colon.
  • The slice must begin at the first character of the value, not at the delimiter or the space after it.
  • For X-DSPAM-Confidence: 0.8475, the colon is at index 18 and the numeric value begins at index 20.
  • Slicing produces the text 0.8475, while float() converts that text into a floating-point value.
  • The find, slice, convert pattern is useful for parsing structured text data.