GCSE · Computer Science · AQA · Spec 8525
Substring
A date is stored as one string, "25/12/2026". A calendar needs the day, month and year separately. How do you cut one string into parts? Two numbers and 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 part of the lesson, not run by the page.
Predict, then check
Same rule as the date, new string. Commit to an answer before you look.
word = "carpenter". In Python, what does word[3:6] give?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How to cut exactly the piece you want out of a string, using just two index numbers.
What you need to know
- What a substring is
- How to write a slice in Python: text[start:end]
- Why the first character of a string is at index 0
- Which characters a slice takes, and which one it leaves out
- How to choose the start and end to extract a particular part
- How to use slicing to separate a string into its parts
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, so the first character is at index 0, and when both numbers fit inside the string, end − start tells you how many characters you get. By choosing a start and an end for each part, slicing can separate one string into several substrings, like the day, month and year of a date. Not every language treats the end number the same way, so check the rule of the language you're using.
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. In Python, date[0:2] gives "25", which is 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 29 September 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.