Testing letters with isalpha() and isupper()

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

Testing letters with isalpha() and isupper()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A Caesar loop reads the character ' ' (a space). What does ' '.isalpha() return?
A False, so the space is left unchanged
B True, so the space is shifted
2. Which line correctly tests whether the character ch is a letter?
A if ch.isalpha():
B if isalpha(ch) == ch:
3. What does '!'.isalpha() return in Python?
A False
B True
4. Why does a Caesar encryption loop check ch.isalpha() before shifting?
A So the loop shifts every character, including spaces
B So only letters are shifted and spaces and punctuation stay the same
5. Which character makes 'G'.isupper() return True?
A G is a letter, so isupper() always returns True for any letter
B G is a capital letter, so it returns True

Match the Pairs

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

Left columnRight column
1ch.isalpha() AReturns True if the letter ch is a capital letter, so the loop knows whether to shift from 'A' or from 'a'
2ch.isupper() Belse: result += ch
3Only shift a character if it is a letter, so spaces and punctuation stay unchanged Cif ch.isalpha():
4Leave a character unchanged when it is not a letter by adding it to the result as it is Disalpha() is a string method, so it is written after the character with a dot. It must be ch.isalpha().
5if ch.isalpha: EThe brackets are missing, so Python checks the method itself and not its result. It must be ch.isalpha().
6if isalpha(ch): FReturns True if the character ch is a letter, and False for spaces, digits and punctuation

Definitions

ch.isalpha()

Fill in the Blanks

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

1.
for ch in message: if ch._______(): result += ch
2.
for ch in message: __ ch.isalpha(): shift = 3
3.
ch = 'q' print(ch._______()) # prints False
4.
if ___ ch.isalpha(): result += ch continue
5.
base = 65 if ch.isupper() else __

Spot the Error

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

Question 1
A for ch in message
B if ch.isalpha():
C result += ch
Question 2
A for ch in text:
B if ch.isalpha:
C print(ch, 'letter')
Question 3
A result = ''
B for ch of text:
C if ch.isalpha():
D result += ch
Question 4
A word = 'Hello'
B if word[0].isUpper():
C print('Capital start')
Question 5
A result = ''
B for ch in text:
C if ch.isalpha():
D result = shift(ch)
E else:
F result += ch

Answer Key

Testing letters with isalpha() and isupper()
Section A: Quick Questions
Q1
A — False, so the space is left unchanged
Q2
A — if ch.isalpha():
Q3
A — False
Q4
B — So only letters are shifted and spaces and punctuation stay the same
Q5
B — G is a capital letter, so it returns True
Section B: Match the Pairs
Q1
ch.isalpha() → Returns True if the character ch is a letter, and False for spaces, digits and punctuation
Q2
ch.isupper() → Returns True if the letter ch is a capital letter, so the loop knows whether to shift from 'A' or from 'a'
Q3
Only shift a character if it is a letter, so spaces and punctuation stay unchanged → if ch.isalpha():
Q4
Leave a character unchanged when it is not a letter by adding it to the result as it is → else: result += ch
Q5
if ch.isalpha: → The brackets are missing, so Python checks the method itself and not its result. It must be ch.isalpha().
Q6
if isalpha(ch): → isalpha() is a string method, so it is written after the character with a dot. It must be ch.isalpha().
Section C: Definitions
Q1
ch.isalpha() — Returns True if the character ch is a letter, so a Caesar loop can shift letters and leave spaces and punctuation unchanged. ch.isupper() checks for capitals.
Section D: Fill in the Blanks
Q1
for ch in message: if ch.isalpha(): result += ch
Q2
for ch in message: if ch.isalpha(): shift = 3
Q3
ch = 'q' print(ch.isupper()) # prints False
Q4
if not ch.isalpha(): result += ch continue
Q5
base = 65 if ch.isupper() else 97
Section E: Spot the Error
Q1
A — A for loop header must end with a colon. It should be: for ch in message:
Q2
B — isalpha is a method, so it needs brackets to run. Without them Python just checks that the method exists, so the test always passes. It should be: if ch.isalpha():
Q3
B — Python loops use the keyword in, not of. It should be: for ch in text:
Q4
B — Python method names are case-sensitive. The method is isupper() with all lowercase letters, not isUpper().
Q5
D — Using = replaces result each time, so only the last character is kept. It should be result += shift(ch) to add to the message.