String Indexing
& Slicing
Learn to access individual characters and substrings using positive indices, negative indices, and Python's powerful slice notation.
What Is a String?
In Python, a string is an ordered sequence of characters enclosed in quotes. Every character lives at a numbered position called an index. Think of a string as a row of labeled boxes: each box holds exactly one character.
Positive Indexing
Use square brackets with a zero-based index to access a single character. The first character is at position 0, the second at 1, and so on.
Negative Indexing
Python lets you count backwards from the end of the string. The last character is -1, the second-to-last is -2, and so on.
Why Two Index Systems?
Positive and negative indexing serve different purposes. Positive indices count from the beginning; negative indices count from the end. Negative indexing is especially handy when you don't know a string's length.
The Slice Syntax
A slice extracts a substring using up to three values separated by colons inside square brackets:
Slices Cut Between Boundaries
Think of slice indices as pointing to the gaps between characters, not the characters themselves. This explains why stop is exclusive.
Indexing vs Slicing
| Indexing | Slicing | |
|---|---|---|
| Syntax | \( s[i] \) | \( s[\text{start}:\text{stop}:\text{step}] \) |
| Returns | A single character (str of length 1) | A new string (substring) |
| Out of range? | Raises IndexError | Silently clips to bounds |
| Example | "abc"[1] → "b" |
"abc"[0:2] → "ab" |
How to Apply Slicing
Example 1: Basic Slices
Example 2: Negative Slices
You can mix positive and negative indices in slices. Negative values count from the end, so -3 means "3 from the end."
Example 3: The Step Parameter
The third value in the slice controls how many positions to jump between characters. A step of 2 grabs every other character. A step of -1 reverses the string.
Default Values in Slicing
You can omit any part of the slice. Python fills in sensible defaults depending on the direction of the step.
| Omission | \( \text{step} > 0 \) | \( \text{step} < 0 \) |
|---|---|---|
| start omitted | 0 (beginning) | -1 (end) |
| stop omitted | len(s) (past end) | -(len(s)+1) (past start) |
| step omitted | Defaults to 1 | |
Practical Patterns
s[:n]s[-n:]s[1:]s[:-1]s[::-1]s[::2]s[1:-1]s[:]Common Mistakes
s[2:5] returns 3 characters (indices 2, 3, 4), not 4.s[len(s)] which is out of range. The last valid index is \( \text{len}(s) - 1 \) or -1.s[-0] is the same as s[0] (the first character), not the last. Use s[-1] for the last character.Question 1 of 5
Given s = "PYTHON", what does s[2] return?
s[0]="P", s[1]="Y", s[2]="T". Remember zero-based counting.Question 2 of 5
Given s = "PYTHON", what does s[-2] return?
s[-1]="N", s[-2]="O". The second-to-last character is "O".Question 3 of 5
Given s = "PYTHON", what does s[1:4] return?
s[1:4] includes indices 1, 2, and 3 (stop is exclusive). That gives us s[1]="Y", s[2]="T", s[3]="H" → "YTH".Question 4 of 5
Given s = "PYTHON", what does s[::-1] return?
s[::-1] reverses the entire string. "PYTHON" reversed is "NOHTYP". The step of -1 traverses from end to start.Question 5 of 5
Given s = "PYTHON", what happens when you run s[10]?