Format check with string methods
A format check tests that text follows an expected pattern, such as 4 digits for a postcode, using string methods like isdigit(), isalpha(), slicing and len().
Understanding Format check with string methods in WA Computer Science ATAR Unit 1
What it does: The slice [:2] takes the first two characters and isalpha() tests that they are all letters, which checks one part of the pattern. isdigit() is True only when every character is a digit, and len(code) == 4 checks the length. Together they test the format. A correct pattern does not prove the data is correct. '0000' fits the format but may not be a real postcode. len() is a built-in function, so you write len(code). Strings have no .len() method. Without () Python refers to the method itself instead of running it, so the check does not test the text for digits.
Common mistakes: A postcode must be 4 digits, so the check needs isdigit(). isalpha() tests for letters and would reject "6000". The line should be ok = ok and code.isdigit(). A single = is assignment and is a syntax error inside a condition. Comparing the length needs ==, so the line should be if len(code) == 4 and ok:.
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
code.isdigit() and len(code) == 4Fill in the Blanks
Spot the Error
Student with a class code? Join your class →