Length check with len()
A length check compares len() of the text with a required length or limit, such as exactly 4 characters for a PIN, to accept or reject the input.
Understanding Length check with len() in WA Computer Science ATAR Unit 1
What it does: A maximum length check uses <= with len() so that 12 or fewer characters pass. Using != with len() catches any PIN that is too short or too long, so the program can reject it. len(pin) gives the number of characters in the text, so len(pin) == 4 tests for exactly 4 characters. Comparing pin itself to 4 compares text with a number. Use == to compare. A single = is assignment and causes a syntax error inside an if statement. len('12345') is 5, which is not equal to 4. The check does not care what the digits are, only how many there are.
Common mistakes: A single = assigns a value, so it cannot compare the length. A length check needs the comparison operator ==, as in if len(pin) == 4:. pin is text, so it is never equal to the number 4. The check must compare the length of the text: if len(pin) == 4:.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 100–314) and word search (Modules 100–470).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
len(pin) == 4Fill in the Blanks
Spot the Error
Student with a class code? Join your class →