Technical skill practice
Python Substring: Slice Bounds, Negative Indexes and Steps
In Python, extract a substring with slicing: text[start:stop:step]. The stop position is excluded. Omitted bounds extend to the relevant edge, negative indexes count from the end, and a step changes which characters are selected. Use in when the question is whether text occurs, rather than where to slice.
Start with a small, explicit task
Inspect the fictional label PK-7348-X. Extract its numeric portion, its prefix and final marker, then reverse it and sample every other character. Check a slice beyond the end alongside a single invalid index to see how their failure behavior differs.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_substring.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
7348 X
P-38X
X-8437-KP
single index rejected
label = "PK-7348-X"
number = label[3:7]
assert number == "7348"
assert label[:2] == "PK"
assert label[-1:] == "X"
assert label[::2] == "P-38X"
assert label[::-1] == "X-8437-KP"
assert label[99:] == ""
assert "7348" in label
try:
label[99]
except IndexError:
result = "single index rejected"
else:
raise AssertionError("expected invalid index")
print(number, label[-1:])
print(label[::2])
print(label[::-1])
print(result)
Explain the result
Positions 3 through 6 contain the four digits; the stop bound 7 is excluded. The omitted start in [:2] begins at the left edge. The negative start in [-1:] selects the final character through the end. A step of two samples alternating positions, and a negative step reverses direction.
The far-right slice returns an empty string, while asking directly for character 99 raises IndexError. This distinction makes slicing convenient for partial extraction, but it can also conceal an invalid expected format. If the label must contain exactly four digits at specific positions, validate that contract rather than assuming a non-crashing slice proves it.
Avoid these mistakes
Keep string indexing distinct from user-perceived characters in general Unicode text: this ASCII label avoids composed-character questions. Do not confuse substring extraction with searching. A slice needs known positions; membership answers only whether a fragment occurs. A zero step is invalid and is not a way to request an empty result.
Questions, answered
Is the stop position included?
No. label[3:7] contains positions 3, 4, 5 and 6.
Why does slicing beyond the end not fail?
The checked slice clips to the available range. A single out-of-range index still raises IndexError.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.