Format check with string methods

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This Format check with string methods worksheet is part of the WA Computer Science ATAR Year 11 — Control Structures and Data Types journey. Print it as a booklet?

Format check with string methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. A student enters '6O12' (letter O instead of zero). What does '6O12'.isdigit() return?
A False
B True
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()
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

code.isdigit() and len(code) == 4

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if code._______() and len(code) == 4:
2.
if code.isdigit() and len(code) == _:
3.
ok = ref[:2]._______() and ref[2:].isdigit()
4.
if len(ref) == 6 ___ ref[:2].isalpha():
5.
code = "0000" valid = code.isdigit() and len(code) == 4 print(_____) # True, but not a real code

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A code = "6000"
B ok = len(code) == 4
C ok = ok and code.isalpha()
D print(ok)
Question 2
A code = "6150"
B ok = code.isdigit()
C if len(code) = 4 and ok:
D print("Format OK")
Question 3
A plate = "AB123"
B letters = plate[:2]
C digits = plate[3:]
D print(letters.isalpha())
E print(digits.isdigit())
Question 4
A code = int(input("Code: "))
B if len(code) == 4:
C if code.isdigit():
D print("Format OK")
Question 5
A code = "AB123"
B a = code[:2].isalpha()
C b = code[2:].isdigit()
D if a or b:
E print("Pattern OK")

Answer Key

Format check with string methods
Section A: Quick Questions
Q1
B — code.isdigit() and len(code) == 4
Q2
A — False
Q3
A — code[:2].isalpha() and code[2:].isdigit()
Q4
B — It tests that the text follows an expected pattern
Q5
A — The format is valid, but the postcode may not be a real one
Section B: Match the Pairs
Q1
code.isdigit() and len(code) == 4 → Checks that a postcode is made up of digits only and is exactly 4 characters long
Q2
plate[:2].isalpha() and plate[2:].isdigit() → Checks that the first two characters are letters and the rest are digits
Q3
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()
Q4
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:
Q5
if code.isdigit and len(code) == 4: → isdigit is missing its brackets, so the method is not called. It should be code.isdigit().
Q6
if len(code) = 4 and code.isdigit(): → A single = assigns a value instead of comparing. It should be len(code) == 4.
Section C: Definitions
Q1
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().
Section D: Fill in the Blanks
Q1
if code.isdigit() and len(code) == 4:
Q2
if code.isdigit() and len(code) == 4:
Q3
ok = ref[:2].isalpha() and ref[2:].isdigit()
Q4
if len(ref) == 6 and ref[:2].isalpha():
Q5
code = "0000" valid = code.isdigit() and len(code) == 4 print(valid) # True, but not a real code
Section E: Spot the Error
Q1
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().
Q2
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:.
Q3
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:].
Q4
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: ").
Q5
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:.