Text as a numbered chain of characters: indexes from 0, negative indexes from the end, slices that stop before the stop — and why strings refuse to be edited in place.
01
Learning objectives
Index single characters with positive and negative indexes and state that counting starts at 0.
Extract sub-strings with [start:stop] and [start:stop:step], including [::-1].
Use len() to measure a string and predict the IndexError at s[len(s)].
Explain string immutability and the TypeError from s[0] = "J".
Build new strings from pieces — concatenation and repetition.
02
Lesson notes
A string is an ordered chain of characters, and Python numbers every character with an index — starting at zero, not one. For word = "PYTHON": word[0] is "P", word[1] is "Y", … word[5] is "N". Asking for word[6] raises IndexError: string index out of range, because a 6-character string owns indexes 0-5. The zero-based rule is the single most quoted fact on ICS papers; drill it until s[0] reflexively means 'first character'.
Negative indexes count from the right end and are legal, documented Python — not a trick: word[-1] is the last character ("N"), word[-2] the second last ("O"), and generally s[-n] is s[len(s)-n]. The pair of rules together gives you a two-way map: position 0 from the left is position -len(s) from the right. Examiners adore questions like 'What is "QUIZ"[-3]?' — trace it calmly: -3 counts U, I from the end... it is "U".
A slice copies out a section: s[start:stop] starts at index start and stops before index stop — the stop character is never included. In "PYTHON", word[1:4] is "YTH" (indexes 1, 2, 3). The stop-excluded rule hides a gift: the slice's length is exactly stop − start. Omit either side: word[:3] means 'from the beginning', word[3:] means 'to the end', word[:] copies everything. A third number, the step, skips: word[::2] takes every second character ("PTO"), and the famous word[::-1] walks backwards — the one-line string reversal every paper includes.
Strings are immutable: once created, no character can be swapped. word[0] = "J" raises TypeError: 'str' object does not support item assignment. This is not a limitation to fight but a design to use — strings are safe to share, hash and cache precisely because nothing can change them underneath you. To 'modify' a string you build a new one: word = "J" + word[1:] produces "JYTHON", and every string method you will meet next lesson (upper(), replace(), strip()…) returns a new string for the same reason.
New strings also come from concatenation (+, joining two strings) and repetition (*, repeating one): "-" * 20 draws a divider, "ha" * 3 is "hahaha", and first + " " + last builds full names. The classic companion error lives here: "Age: " + 18 is a TypeError because str + int is undefined — cast with str(18) or, better, use an f-string next lesson. With indexing, slicing, len() and these two operators, you can already dissect and rebuild any text the board can throw at you.
03
Worked code examples
Indexes from both ends
Forward indexes start at 0 (word[3] is the 4th character, H); negative indexes count from the end (word[-3] is H as well). len() reports 6 characters owning indexes 0-5.
word = "PYTHON"
print(word[0], word[3], word[5])
print(word[-1], word[-3])
print(len(word))
Output
P H N
N H
6
Slices: start included, stop excluded
word[1:4] stops before 4, so only three characters come out — length equals stop − start. Omitted sides mean 'from the edge', and step -1 walks the string backwards.
word = "PYTHON"
print(word[1:4]) # indexes 1,2,3
print(word[:3]) # beginning to before 3
print(word[3:]) # from 3 to the end
print(word[::-1]) # step -1: reversed
Output
YTH
PYT
HON
NOHTYP
Immutability — build a new string instead
Strings cannot be edited in place. The fix creates a new string: word = "J" + word[1:] gives "JYTHON", rebinding the name to the fresh value.
word = "PYTHON"
word[0] = "J"
Output
TypeError: 'str' object does not support item assignment
04
Key terminology
index
A character's position in a string, counted from 0; negative indexes count from the end.
slice
A copied section of a string, s[start:stop:step]; the stop index is excluded.
step
The optional third slice number — how far to jump each time; -1 reverses the string.
immutable
Cannot be changed after creation — strings are immutable; 'editing' means building a new string.
len()
Built-in returning the number of characters; the last valid index is len(s) − 1.
05
Common misconceptions
Common mistake
word[1] is the first character.
Correct understanding
Indexing starts at 0, so word[0] is first and word[1] is the second character — the classic examiner trap.
Common mistake
The slice word[1:4] includes word[4].
Correct understanding
Slices stop BEFORE the stop index: word[1:4] yields indexes 1, 2, 3 — exactly three characters.
Common mistake
You can fix one letter with word[0] = "J".
Correct understanding
Strings are immutable — that line raises a TypeError. Build a new string: "J" + word[1:].