GCSE · Computer Science · AQA · Spec 8525
Substring
Your calendar stores "25/12/2026" as one string, but needs the day, month and year apart. How do you cut them out? Two numbers, one rule.
Computer Science · Watch the slices
One string, three pieces
This date is stored as a single string. Step through the program and watch three slices cut out the day, the month and the year. The comment on line 1 shows each character's position number, called its index.
Predict before you reach step 2: date[0:2] takes which characters of "25/12/2026"?
1 # index 01234567892 date = "25/12/2026"3 day = date[0:2]4 month = date[3:5]5 year = date[6:10]6 print(day, month, year)
| variable | slice | indices taken | value |
|---|---|---|---|
| date | (whole string) | 0 to 9 | "25/12/2026" |
Output
Step 1: The whole date is one string of 10 characters. Look at the comment above it: the first character, 2, sits at index 0, not index 1. In Python, and in many other programming languages, counting starts at 0. That's why the last character, 6, is at index 9 rather than 10.
Step through the slices. Every value in the table is written into the lesson; the page doesn't run the code.
Predict, then check
Same rule as the date, brand-new string. Pick one before you look. No peeking.
word = "carpenter". In Python, what does word[3:6] give?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Cut exactly the piece you want out of a string, using just two index numbers.
What you need to know
- A substring is just a piece of a longer string, like "2026" inside "25/12/2026".
- Each character sits at a numbered position, called its index.
- Python, and many other programming languages, count those positions from 0, not 1.
Have a goYour mate insists the P in "Python" is at index 1. In Python, which letter is really at index 1?
y. The P is at index 0.
Counting starts at 0, so index 1 is the second character. Your mate counted from 1, like everyday counting, so every position they give comes out one too high.
- You cut a substring out by slicing. In Python that looks like text[start:end].
- The character at start gets taken. The character at end doesn't: the slice stops just before it.
Have a gos = "robot". s[0:3] stops before its end index. Which letter is sitting at that end index, just outside the slice?
The second o, at index 3. So s[0:3] gives "rob".
r, o, b are at 0, 1 and 2, so index 3 is the second o. If you said b, you counted from 1, which shifts every index by one.
- If both numbers fit inside the string, end − start is how many characters you get.
- Want a particular part? Start on its first character and end one past its last.
Have a goname = "Kai.Lee". "Lee" runs from index 4 to index 6. What end number do you write to cut it out?
7, so name[4:7].
The end must be one past the last character you want: 6 + 1 = 7. Writing 6 would stop before the final e and give "Le".
- One slice per part, skipping separators like / or :, splits a whole string into pieces.
- Other languages can treat that end number differently, so always check the rule of the one you're using.
The big picture
A substring is a part of a string. In Python you get one by slicing: text[start:end] takes the characters from index start up to, but not including, index end. Indexing starts at 0 in Python and many other programming languages, and when both numbers fit inside the string, end − start tells you how many characters you get. One slice per part separates a string into substrings, like the day, month and year of a date. Not every language treats the end number the same way, so check your language's rule.
Key points
Worked example
Problem
A program stores a file name as filename = "holiday.jpg". Its indices run from 0 to 10, and the dot is at index 7. Using Python slicing, separate it into the name, "holiday", and the extension, "jpg".
⚠ Watch out
Treating the end number as the last character you get. Feels natural; isn't the rule. In Python, date[0:2] gives "25", indices 0 and 1, because the slice stops before index 2. If the last character you want is at index 4, the end has to be 5.
Memory hook
Start ON it, stop BEFORE it, and count from zero.
Check yourself
word = "sandwich". Without looking back: what does word[1:4] give in Python? Which index does the slice start on, and which index does it stop before?
Flashcards
(8)What is a substring?
In Python, what is the index of the first character of a string?
In Python, which characters does text[start:end] take?
How can you work out how many characters a Python slice takes?
Is the second number in text[start:end] a length?
How do you choose the end number to extract a part of a string?
How can slicing separate a string into its parts?
Does every programming language treat the end of a substring the same way as Python?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.