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")
passwordlen(password)len(password) < 8output
""

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.

1 / 9

Press Next one step at a time. Say the length out loud before each len row appears.

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

Exam line: len gives back a whole number. Once you have that number, you can compare it (< 8), use it to control a loop, or print it.
Watch out: len counts characters, not letters. The space and the ! in "red fox!" each add 1, which is exactly why the third attempt gets through.

Predict, then check

Count first, then pick. Commit to one answer before you look.

What does len("Year 11!") give back?

Computer Science · Strings

Which line breaks the rule?

The rule: a username can be no more than 12 characters long. Priya tests this program with a username that is exactly 12 characters, and it prints "Too long". Which line is wrong?

A program with one bug — which line goes wrong?

Exam line: Turn the words into a comparison, then test the boundary number itself: "at least 8" is >= 8, "more than 8" is > 8, "no more than 12" is <= 12.

Your turn to code

Write a nickname checker

Write a program that asks the user for a nickname. It must keep asking until the nickname is between 3 and 10 characters long (3 and 10 are both allowed). Then it outputs Welcome, followed by the nickname. You can write in Python or pseudo-code. [4 marks]

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

Exam line: Credit comes from each separate job the code does: getting the input, measuring it, testing it correctly and repeating when it fails.

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

1Length is a count of characters, not letters and not words: len("Hi, Sam") is 7.
2Digits inside a string are characters: len("2024") is 4, not 2024.
3The quote marks around a string are not part of it, so len("") is 0.
4Turn a rule into a comparison carefully: "at least 8" is len(s) >= 8, "no more than 12" is len(s) <= 12.
5Other languages spell it differently (LEN(s), s.length), but the idea is identical: count the characters, get a whole number.

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?
A whole number (an integer): how many characters are in the string s.
Do spaces and punctuation count towards a string's length?
Yes. Every character counts: len("a b!") is 4.
What is the length of the empty string ""?
0. It contains no characters, and the quote marks are not part of the string.
What is len("2024")?
4. Digits in a string are just characters, one each. It is not the number 2024.
Write the condition for "s has at least 8 characters".
len(s) >= 8. (len(s) > 8 would wrongly reject exactly 8 characters.)
Why is it useful that len gives back a number?
A program can compare it in an if or a while, do arithmetic with it, or print it. For example: keep asking while len(password) < 8.

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.