GCSE · Computer Science · AQA · Spec 8525
String length
Ever been told "Password too short" when you were sure it wasn't? A program counted your characters. Here's how it counts, and why one space can flip the answer.
Computer Science · Strings
Is this password long enough?
This Python program keeps asking for a password until it has at least 8 characters. Three attempts are typed in. Step through and watch len(password) drive the loop.
Before you step: the third attempt, "red fox!", has only six letters. Will the loop let it through?
1 password = input("Choose a password: ")2 while len(password) < 8:3 print("Too short")4 password = input("Choose a password: ")5 print("Accepted:", len(password), "characters")
| password | len(password) | len(password) < 8 | output |
|---|---|---|---|
| "" |
Output
Step 1: Attempt 1: the user just presses Enter, so password holds the empty string. The quote marks only show where the string starts and ends. They are not characters in it.
Press Next one step at a time. Say the length out loud before each len row appears.
Predict, then check
Count first, then pick. Commit to one answer before you look.
What does len("Year 11!") give back?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How a program counts the characters in a piece of text, and then uses that number to make decisions.
What you need to know
- len(s) gives back a whole number (an integer): how many characters are in the string s.
- Every character counts: letters, digits, spaces and punctuation each add 1.
- The empty string "" has no characters, so its length is 0.
- Because the length is a number, a program can compare it in an if statement or use it to control a loop.
The big picture
The length operation takes a string and gives back a whole number: how many characters it contains. Every character counts: letters, digits, spaces and punctuation. A string with nothing in it has length 0. Because the answer is a number, a program can test it in an if statement or a loop, for example to keep asking for a password until it is long enough.
Key points
Worked example
Problem
A messaging app only sends messages of 15 characters or fewer. What does this program output? message = "See you at 3:15!" limit = 15 if len(message) > limit: print("Too long by", len(message) - limit) else: print("Sent")
⚠ Watch out
Counting only the letters (or skipping the spaces and punctuation). len("red fox!") is 8, not 6: the space and the ! are characters too, and a wrong count sends the program down the wrong branch.
Memory hook
Picture the string written one character per box: letters, digits, spaces and symbols each get their own box. len is the number of boxes. No boxes at all? Length 0.
Check yourself
Without running any code: what is len("") and what is len(" ") (two spaces)? Why are they different, even though neither shows any letters?
Flashcards
(6)What does len(s) give back?
Do spaces and punctuation count towards a string's length?
What is the length of the empty string ""?
What is len("2024")?
Write the condition for "s has at least 8 characters".
Why is it useful that len gives back a number?
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.