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.
text = "COMPUTER"
# 01234567 ← positions
print(text[0]) # C — one character
print(text[0:3]) # COM — from 0, stopping BEFORE 3
print(text[3:8]) # PUTER
print(len(text)) # 8 — the number of charactersString text = "COMPUTER";
// 01234567 ← positions
System.out.println(text.charAt(0)); // C — one character
System.out.println(text.substring(0, 3)); // COM — from 0, stopping BEFORE 3
System.out.println(text.substring(3, 8)); // PUTER
System.out.println(text.length()); // 8 — the number of charactersFree 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 exam-style 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.
email = "ana.silva@school.org"
at = email.find("@") # 9
user = email[:at] # ana.silva
domain = email[at + 1:] # school.org — +1 skips the @ itself
print(user, domain)
# Building a student code from a name and a year
name = "Ana Silva"
first, last = name.split(" ")
code = last[:3].upper() + first[0].upper() + "26"
print(code) # SILA26String email = "ana.silva@school.org";
int at = email.indexOf("@"); // 9
String user = email.substring(0, at); // ana.silva
String domain = email.substring(at + 1); // school.org — +1 skips the @ itself
System.out.println(user + " " + domain);
// Building a student code from a name and a year
String name = "Ana Silva";
String[] parts = name.split(" ");
String code = parts[1].substring(0, 3).toUpperCase()
+ parts[0].substring(0, 1).toUpperCase() + "26";
System.out.println(code); // SILA26Why +1 after find
find returns the position of the @ itself. Slicing from there would include it.
Adding 1 starts one character later, which is what you actually want.
Always handle not found
find returns -1 when the text is not there, and -1 used as a position silently gives the wrong answer rather than an error.
Check for -1 before using the result.
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 answer broken into clear points, with guidance on how each point earns marks.