← WA Computer Science ATAR Unit 1

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:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Which condition checks that a postcode is exactly 4 digits?
code.isdigit() and len(code) == 4code.isalpha() and len(code) == 4
Matches
if code.isdigit and len(code) == 4:
↓
isdigit is missing its brackets, so the method is not called. It should be code.isdigit().
Applying
Meanings
Term
code.isdigit() and len(code) == 4
Meaning
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().
Practice
postcode = "6000"
Testing
Gaps
if code.() and len(code) == 4:
Check Answer
Spots
2ok = len(code) == 4
3ok = ok and code.isalpha()

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Dolphin' badge — Reef Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which condition checks that a postcode is exactly 4 digits?
A. code.isalpha() and len(code) == 4
B. code.isdigit() and len(code) == 4 ✓
isdigit() is True only when every character is a digit, and len(code) == 4 checks the length. Together they test the format.
2. A student enters '6O12' (letter O instead of zero). What does '6O12'.isdigit() return?
A. False ✓
B. True
The letter O is not a digit, so isdigit() returns False. The format check catches it.
3. Which code tests the pattern 'two letters then digits' for a code like 'WA123'?
A. code[:2].isalpha() and code[2:].isdigit() ✓
B. code[:2].isdigit() and code[2:].isalpha()
Slice the first two characters and check they are letters with isalpha(). Then slice the rest and check they are digits with isdigit().
4. Which statement about a format check is true?
A. It proves the data is correct and really exists
B. It tests that the text follows an expected pattern ✓
A format check only tests the pattern. It does not prove the data is true or real.
5. A user enters the postcode '0000'. It passes code.isdigit() and len(code) == 4. What does this show?
A. The format is valid, but the postcode may not be a real one ✓
B. The postcode must be correct because it passed the check
A correct pattern does not prove the data is correct. '0000' fits the format but may not be a real postcode.

Match the Pairs

code.isdigit() and len(code) == 4 → Checks that a postcode is made up of digits only and is exactly 4 characters long
plate[:2].isalpha() and plate[2:].isdigit() → Checks that the first two characters are letters and the rest are digits
A format check can use slicing to test only part of a string, such as the first two characters being letters → id_code[:2].isalpha()
A correct pattern does not prove the data is correct, because 0000 passes the 4-digit postcode check but may not be a real postcode → if code.isdigit() and len(code) == 4:
if code.isdigit and len(code) == 4: → isdigit is missing its brackets, so the method is not called. It should be code.isdigit().
if len(code) = 4 and code.isdigit(): → A single = assigns a value instead of comparing. It should be len(code) == 4.

Key Term

code.isdigit() and len(code) == 4
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().

Fill in the Blanks

1. if code.isdigit() and len(code) == 4:
Hint: Type the string method that checks every character is a numeral
2. if code.isdigit() and len(code) == 4:
Hint: Type how many characters a postcode must have
3. ok = ref[:2].isalpha() and ref[2:].isdigit()
Hint: Type the string method that checks the first two characters are all letters
4. if len(ref) == 6 and ref[:2].isalpha():
Hint: Type the Boolean operator that needs both the length check and the letter check to pass
5. code = "0000" valid = code.isdigit() and len(code) == 4 print(valid) # True, but not a real code
Hint: Type the variable that holds the result of the format check

Spot the Error

Question 1
A code = "6000"
B ok = len(code) == 4
C ok = ok and code.isalpha()
D print(ok)
Line C — 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().
Question 2
A code = "6150"
B ok = code.isdigit()
C if len(code) = 4 and ok:
D print("Format OK")
Line C — 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:.
Question 3
A plate = "AB123"
B letters = plate[:2]
C digits = plate[3:]
D print(letters.isalpha())
E print(digits.isdigit())
Line C — The pattern is two letters then digits, so the digits start at index 2. plate[3:] skips the first digit and checks the wrong part. It should be digits = plate[2:].
Question 4
A code = int(input("Code: "))
B if len(code) == 4:
C if code.isdigit():
D print("Format OK")
Line A — A format check works on text. Converting to int means len(code) and code.isdigit() cause errors because integers have neither. The line should be code = input("Code: ").
Question 5
A code = "AB123"
B a = code[:2].isalpha()
C b = code[2:].isdigit()
D if a or b:
E print("Pattern OK")
Line D — The pattern is only correct when BOTH the letters part and the digits part pass. Using or lets a code with only one correct part through. It should be if a and b:.

Student with a class code? Join your class →