The big idea: A string is characters in order, and every one has a position.
A substring is a run of those positions taken out as a string of its own.
Positions start at 0: The first character is at position 0, not 1.
So in "HELLO", H is 0, E is 1, L is 2, L is 3 and O is 4. Nearly every substring error starts here.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
The end position is not included: text[2:5] gives the characters at positions 2, 3 and 4 — not 5.
That sounds awkward until you notice the useful consequence: the length of the slice is simply end minus start. 5 − 2 = 3 characters.
Taking part of it
- text[a:b] — from a, up to but not including b
- text[:b] — from the start
- text[a:] — to the end
- text[-1] — the last character, counting back
Finding and measuring
- len(text) — how many characters
- text.find("x") — the position where x starts, or -1
- "x" in text — True or False
- Find before you slice, when you do not know the position
Changing it
- text.upper() and text.lower()
- text.replace(a, b) — swap one run for another
- text.split(",") — break into a list at each comma
- All of these return a new string; the original is unchanged
Strings do not change in place: text.upper() returns an uppercase copy — it does not modify text.
If you want to keep it, you must assign it: text = text.upper(). Forgetting is a very common bug.
Practice with real exam questions
Answer exam-style questions and get AI feedback that shows you exactly what examiners want to see in a full-marks response.
Find the position, then slice: Most real problems do not give you the position. You find it first, then use it.
Splitting an email at the @ means finding the @, then slicing either side of it.
Why +1 after find
Always handle not found
How this is tested — you must get the positions right — counting from 0, and excluding the end. It comes up two ways:
Paper 2 — working with code
- Construct code to extract or build a substring, 3-5 marks
- State what a given slice produces
- Explain an off-by-one error
Paper 2 — the algorithmic-thinking question
- Reason about positions with no code shown
- Say what a described manipulation would produce
The classic trap: Off by one. Positions start at 0, and the end of a slice is not included. Write the positions above the string on your paper before you answer anything.
A product code looks like "BK-2026-1147". Write code to extract the type (BK), the year (2026) and the number (1147), without assuming fixed positions.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.