Slicing Strings
A string is a sequence of individual characters that can be accessed one at a time using the bracket operator.
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
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- 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.
Loading the simulation…
A String as a Sequence
A string is not a single indivisible item. It is a sequence of individual characters. Python lets you access those characters one at a time by placing an index inside brackets after the string.
For example, imagine the string "CODE". Its characters can be considered separately: C, O, D, and E. To choose one character, you identify its integer index and use the bracket operator with that index.
Positions Begin at Zero
Python uses zero-based indexing. The first character is at index 0, not index 1. An index represents an offset from the start of the string: the first character has an offset of zero because no characters come before it. The next character has an offset of one.
When you count characters for human communication, you may naturally say first, second, and third. When you use Python indices, translate those positions to 0, 1, and 2.
Choosing One Character
The bracket operator uses the form string_name[index], where index must be an integer. Python uses the integer to select one character from the string.
Selecting a Character from CODE
Find the character selected by the expression "CODE"[2].
- 1Step 1: Map the indices
The characters have zero-based indices: C is at 0, O is at 1, D is at 2, and E is at 3.
- 2Step 2: Read the requested index
The bracket contains the integer 2, so the third character is selected.
- 3Step 3: Identify the character
The character at index 2 is D.
"CODE"[2] selects D.
Fixing the First-Character Error
A common indexing mistake is to assume that the first character is at index 1. In Python, index 1 identifies the second character because one character comes before it at index 0.
Common mistake Using index 1 when you intend to select the first character.
What it looks likeFor the string "DATA", treating index 1 as the first position selects A instead of D.
Why it failsPython starts string indices at 0, so index 1 is the second character.
Do this insteadUse index 0 for the first character.
Common mistake Using a non-integer as an index.
What it looks likeTrying to use a float as the index.
Why it failsThe string index must be an integer.
Do this insteadReplace the non-integer index with an integer.
Practice the Mapping
For the string "PYTHON", identify the character at index 0, the character at index 1, and the character at index 4. Then explain why index 1 does not refer to the first character.
Hints
- Begin by writing the indices from 0 upward.
- The first character receives index 0.
- Move one position to the right for each increase in the index.
Checking the Practice Mapping
Map the requested indices in "PYTHON" to their characters.
- 1Index 0
The first character is P.
- 2Index 1
The second character is Y.
- 3Index 4
Counting from zero gives P at 0, Y at 1, T at 2, H at 3, and O at 4.
Index 0 selects P, index 1 selects Y, and index 4 selects O. Index 1 is not the first character because the first character is assigned index 0.
Key Takeaways
- 1A string is a sequence of individual characters.
- 2Characters can be accessed one at a time with the bracket operator.
- 3The form
string_name[index] uses an integer index to select a character. - 4Python uses zero-based indexing, so the first character is at index 0 and the second character is at index 1.
- 5A non-integer index, such as a float, raises a
TypeError.
At a glance
- A string is a sequence of individual characters that can be accessed separately.
- Use the bracket operator with an integer index to select a character.
- Python counts string positions from zero, not one.
- Index 0 identifies the first character; index 1 identifies the second.
- Using a non-integer as an index raises a
TypeError.