← WA Computer Science ATAR Unit 1

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

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 PIN is exactly 4 characters long?
len(pin) == 4pin == 4
Matches
if pin == 4:
↓
The PIN text is compared directly with the number 4. It should compare its length: len(pin) == 4.
Applying
Meanings
Term
len(pin) == 4
Meaning
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.
Practice
pin = "4821"
Testing
Gaps
if (pin) == 4:
Check Answer
Spots
1if len(pin) = 4:
2 print('PIN accepted')

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

The 'Clownfish' badge — Reef Rookie, 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 PIN is exactly 4 characters long?
A. pin == 4
B. len(pin) == 4 ✓
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.
2. A password must have a minimum length of 8 characters. Which check accepts it?
A. len(password) >= 8 ✓
B. len(password) <= 8
A minimum length means 8 or more, so use >=. Using <= would only accept passwords of 8 characters or fewer.
3. A username can be at most 12 characters. Which check accepts it?
A. len(username) >= 12
B. len(username) <= 12 ✓
A maximum length means 12 or fewer characters, so use <=.
4. A length check tests the...
A. number of characters in the input ✓
B. meaning of the input
A length check only counts characters with len(). It cannot tell whether the data makes sense, only whether it is the right size.
5. Which line is a correct length check in Python?
A. if len(code) == 6: ✓
B. if len(code) = 6:
Use == to compare. A single = is assignment and causes a syntax error inside an if statement.

Match the Pairs

len(pin) == 4 → Checks that the PIN is exactly 4 characters long
len(password) >= 8 → Checks that the password is at least 8 characters long
Accept a username only if it has no more than 12 characters (maximum length) → if len(username) <= 12:
Reject a PIN that is not exactly 4 characters long → if len(pin) != 4:
if pin == 4: → The PIN text is compared directly with the number 4. It should compare its length: len(pin) == 4.
if len(password) > 8: → Using > means an 8-character password is rejected. For a minimum of 8 it should be len(password) >= 8.

Key Term

len(pin) == 4
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.

Fill in the Blanks

1. if len(pin) == 4:
Hint: Type the built-in function that counts the characters in a text value.
2. if len(pin) == 4:
Hint: Type the comparison operator that tests for an exact match in length.
3. if len(password) >= 8:
Hint: Type the operator that means the length must be at least the minimum.
4. valid = len(username) <= 12
Hint: Type the operator that allows a length up to and including the maximum.
5. pin = input("Enter 4-digit PIN: ") if len(pin) == 4: print("PIN accepted")
Hint: Type the required length of the PIN, as shown in the prompt.

Spot the Error

Question 1
A if len(pin) = 4:
B print('PIN accepted')
C else:
D print('PIN rejected')
Line A — A single = assigns a value, so it cannot compare the length. A length check needs the comparison operator ==, as in if len(pin) == 4:.
Question 2
A pin = input('PIN: ')
B valid = False
C if pin == 4:
D valid = True
E print(valid)
Line C — 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:.
Question 3
A pw = input('Password: ')
B if len(pw) <= 8:
C print('Strong enough')
D else:
E print('Too short')
Line B — A minimum length check must accept passwords that are 8 or more characters. <= accepts only short ones, so it should be >=.
Question 4
A name = input('Username: ')
B limit = 12
C if len[name] <= limit:
D print('OK')
E else:
F print('Too long')
Line C — len is a function, so it is called with round brackets, not square brackets. It should be len(name).
Question 5
A pin = input('Enter PIN: ')
B while len(pin) != 4:
C print('Must be 4 digits')
D pin == input('Try again: ')
E print('PIN saved')
Line D — A double == compares values instead of storing the new input, so pin never changes and a wrong PIN loops forever. It should be a single = to assign: pin = input('Try again: ').

Student with a class code? Join your class →