01 / 20
P y t h n 0 1 2 3 4

String Indexing
& Slicing

Learn to access individual characters and substrings using positive indices, negative indices, and Python's powerful slice notation.

Python

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.

P y t h o n ! 0 1 2 3 4 5 6 positive index →

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.

word = "Python!" print(word[0]) # P print(word[3]) # h print(word[6]) # !

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.

P y t h o n ! 0 1 2 3 4 5 6 -7 -6 -5 -4 -3 -2 -1 ← negative index
word = "Python!" print(word[-1]) # ! (last character) print(word[-3]) # o print(word[-7]) # P (same as word[0])

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.

Positive Index
Starts at 0 from the left. Use when you know the exact position from the start.
Negative Index
Starts at -1 from the right. Use to grab characters from the end without computing length.

The Slice Syntax

A slice extracts a substring using up to three values separated by colons inside square brackets:

s[start : stop : step]
\( \text{start} \)
Index where the slice begins (inclusive). Defaults to 0.
\( \text{stop} \)
Index where the slice ends (exclusive). Defaults to len(s).
\( \text{step} \)
How many indices to skip. Defaults to 1. Use -1 to reverse.

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.

0 1 2 3 4 5 6 7 P y t h o n ! start=2 stop=5 "tho"

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

1
Identify the target substring you need: which characters from start to end?
2
Pick the start index: the position of the first character you want (inclusive).
3
Pick the stop index: one past the last character you want (exclusive).
4
Choose a step (optional): use 1 for every character, 2 for every other, or -1 to reverse.
5
Write the expression: \( s[\text{start}:\text{stop}:\text{step}] \). Omit any value to use its default.

Example 1: Basic Slices

s = "ABCDEFGH" print(s[0:3]) # "ABC" (indices 0, 1, 2) print(s[3:6]) # "DEF" (indices 3, 4, 5) print(s[:4]) # "ABCD" (start defaults to 0) print(s[5:]) # "FGH" (stop defaults to end) print(s[:]) # "ABCDEFGH" (full copy)
A B C D E F G H 0 1 2 3 4 5 6 7 s[0:3] → "ABC"

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

s = "ABCDEFGH" print(s[-3:]) # "FGH" (last 3 characters) print(s[:-2]) # "ABCDEF" (everything except last 2) print(s[-5:-2]) # "DEF" (from -5 up to -2) print(s[2:-1]) # "CDEFG" (mix positive and negative)

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.

s = "ABCDEFGH" print(s[::2]) # "ACEG" (every 2nd character) print(s[1::2]) # "BDFH" (every 2nd, starting at 1) print(s[::-1]) # "HGFEDCBA" (full reverse) print(s[5:1:-1]) # "FEDC" (reverse from 5 down to 2)

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

First N chars
s[:n]
Last N chars
s[-n:]
Remove first
s[1:]
Remove last
s[:-1]
Reverse
s[::-1]
Every other
s[::2]
Middle
s[1:-1]
Copy
s[:]

Common Mistakes

✕ Off-by-one error
Forgetting that the stop index is exclusive. s[2:5] returns 3 characters (indices 2, 3, 4), not 4.
✕ IndexError on access
Using s[len(s)] which is out of range. The last valid index is \( \text{len}(s) - 1 \) or -1.
✕ Confusing -0 with -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?

Question 2 of 5

Given s = "PYTHON", what does s[-2] return?

Question 3 of 5

Given s = "PYTHON", what does s[1:4] return?

Question 4 of 5

Given s = "PYTHON", what does s[::-1] return?

Question 5 of 5

Given s = "PYTHON", what happens when you run s[10]?