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 0123456789
2 date = "25/12/2026"
3 day = date[0:2]
4 month = date[3:5]
5 year = date[6:10]
6 print(day, month, year)
variablesliceindices takenvalue
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.

1 / 5

Step through the slices. Every value in the table is part of the lesson, not run by the page.

Step 1 of 5: 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..

Watch out: The second number in a slice is where the slice stops, and the character at that index is not taken. date[0:2] stops before index 2, so it takes indices 0 and 1 only.

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?

Computer Science · Your turn to choose

Pick the numbers

The string time = "07:45:30" holds hours, minutes and seconds. Its indices are: 0 → 0, 1 → 7, 2 → :, 3 → 4, 4 → 5, 5 → :, 6 → 3, 7 → 0. Separate it into three substrings.

  1. time = "07:45:30"Each part is two characters long, with a colon between parts.
  2. missing step
Which line is step 2?

Computer Science · Write it, then mark it

Now write your own

A shop stores each product code as a string, for example code = "BK-2291-UK". The four-digit number always sits at indices 3 to 6. Write Python code that extracts the four-digit number from code, stores it in a variable called number and outputs it. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A substring is a part of a string. "25" is a substring of "25/12/2026".
2In Python, and many other programming languages, indexing starts at 0: the first character of a string is at index 0.
3Slicing cuts a substring out of a string. In Python you write text[start:end].
4The character at start is included. The slice stops before end, so the character at end is not included.
5When both numbers fit inside the string, end − start tells you how many characters a slice takes: date[6:10] takes 4.
6To extract a part, start on the index of its first character and end one past the index of its last character.
7Slicing can separate a string into substrings: one slice for each part, skipping separators such as / or :.
8Not every programming language treats the end of a substring the same way, so check the rule of the language you're using.

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?
A part of a string. For example, "12" is a substring of "25/12/2026".
In Python, what is the index of the first character of a string?
0. Indexing starts at 0 in Python and many other programming languages.
In Python, which characters does text[start:end] take?
The characters from index start up to, but not including, index end.
How can you work out how many characters a Python slice takes?
Subtract: end − start, as long as both numbers fit inside the string. For a 10-character string, text[3:8] takes 5 characters.
Is the second number in text[start:end] a length?
No. It's a position: the index the slice stops before.
How do you choose the end number to extract a part of a string?
Find the index of the part's last character, then add 1.
How can slicing separate a string into its parts?
Use one slice per part, each with its own start and end, and skip any separators such as / or :.
Does every programming language treat the end of a substring the same way as Python?
No. Check the rule of the language you're using before you choose your numbers.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.