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?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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(). |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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().if code.isdigit() and len(code) == 4:if code.isdigit() and len(code) == 4:ok = ref[:2].isalpha() and ref[2:].isdigit()if len(ref) == 6 and ref[:2].isalpha():code = "0000"
valid = code.isdigit() and len(code) == 4
print(valid) # True, but not a real code