← ACARA Digital Technologies Years 9–10

Encrypting a message with a Caesar loop in Python

Shifts an upper-case letter ch along the alphabet by shift, wrapping past Z back to A. A for loop applies it to each letter and builds the ciphertext with +=, skipping spaces.

Understanding Encrypting a message with a Caesar loop in Python in ACARA Digital Technologies Years 9–10

What it does: The += operator joins each shifted letter onto the end of result, so the ciphertext grows as the for loop runs. def creates a reusable function with the message (text) and the key (shift) as inputs, so the same loop works for any message. The - 65 turns A-Z into 0-25, % 26 wraps the number around, and + 65 turns it back into a letter. Without the wrap, Z would shift to a symbol, not a letter. Spaces and punctuation are not letters, so a Caesar cipher keeps them as they are. This is why we check with isalpha() first.

Common mistakes: Upper-case letters start at ord('A') = 65, so subtract 65 to get a position from 0 to 25. Subtracting 97 is for lower-case letters and would give negative numbers here. It should be n = ord(ch) - 65. range() needs a number, not a string, so this causes a TypeError. A for loop can go straight over the characters of the message: for ch in text:.

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 860–875) and word search (Modules 860–944).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line shifts an upper-case letter ch by shift and wraps past Z back to A?
chr((ord(ch) - 65 + shift) % 26 + 65)chr(ord(ch) + shift)
Matches
result = chr((ord(ch) - 65 + shift) % 26 + 65)
↓
Uses = instead of += so each letter replaces the result and only the last letter is kept
Applying
Meanings
Term
chr((ord(ch) - 65 + shift) % 26 + 65)
Meaning
Shifts an upper-case letter ch along the alphabet by shift, wrapping past Z back to A. A for loop applies it to each letter and builds the ciphertext with +=, skipping spaces.
Practice
def caesar(text, shift):
Testing
Gaps
new = chr(((ch) - 65 + shift) % 26 + 65)
Check Answer
Spots
3 if ch.isalpha():
4 n = ord(ch) - 97

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

The 'Ermine' badge — Tundra 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 line shifts an upper-case letter ch by shift and wraps past Z back to A?
A. chr(ord(ch) + shift)
B. chr((ord(ch) - 65 + shift) % 26 + 65) ✓
The - 65 turns A-Z into 0-25, % 26 wraps the number around, and + 65 turns it back into a letter. Without the wrap, Z would shift to a symbol, not a letter.
2. How should a Caesar loop build up the ciphertext string?
A. result += chr((ord(ch) - 65 + shift) % 26 + 65) ✓
B. result = chr((ord(ch) - 65 + shift) % 26 + 65)
+= adds each new letter onto the end of result. Using = replaces result every time, so only the last letter would be kept.
3. What should the loop do with a space in the message, such as the gap in 'HI THERE'?
A. Leave it unchanged and add it to the result ✓
B. Shift it by the same amount as the letters
Spaces and punctuation are not letters, so a Caesar cipher keeps them as they are. This is why we check with isalpha() first.
4. Which test lets the loop skip spaces and punctuation?
A. if ch.isupper() == False:
B. if ch.isalpha(): ✓
isalpha() is True only for letters, so spaces and punctuation go to the else branch and are copied unchanged. A check for not upper-case would also wrongly catch lower-case letters.
5. Which is the correct first line to wrap the cipher in a reusable function?
A. def caesar(text, shift): ✓
B. def caesar(text shift)
A function header needs the def keyword, parentheses with parameters separated by a comma, and a colon at the end. The wrong version is missing the comma and the colon.

Match the Pairs

chr((ord(ch) - 65 + shift) % 26 + 65) → Shifts the upper-case letter ch forward by shift places and wraps from Z back to A
if ch.isalpha(): → Only shifts the character if it is a letter, so spaces and punctuation can be left unchanged
Build up the ciphertext one character at a time by adding each new character to a result string → result += chr((ord(ch) - 65 + shift) % 26 + 65)
Wrap the loop in a function so you can encrypt any text with any shift → def caesar(text, shift):
result = chr((ord(ch) - 65 + shift) % 26 + 65) → Uses = instead of += so each letter replaces the result and only the last letter is kept
chr((ord(ch) - 65 + shift) + 65) → Leaves out % 26 so letters near the end of the alphabet do not wrap back to A and become symbols

Key Term

chr((ord(ch) - 65 + shift) % 26 + 65)
Shifts an upper-case letter ch along the alphabet by shift, wrapping past Z back to A. A for loop applies it to each letter and builds the ciphertext with +=, skipping spaces.

Fill in the Blanks

1. new = chr((ord(ch) - 65 + shift) % 26 + 65)
Hint: Type the function that turns the letter ch into its number code.
2. new = chr((ord(ch) - 65 + shift) % 26 + 65)
Hint: Type the operator that gives the remainder, so letters wrap from Z back to A.
3. for ch in text: if ch.isalpha(): result += shifted
Hint: Type the string method that checks a character is a letter, so spaces are skipped.
4. def caesar(text, shift): result = "" return result
Hint: Type the keyword that starts a function definition.
5. result = "" for ch in text: result += new
Hint: Type the operator that adds the new letter onto the end of the ciphertext string.

Spot the Error

Question 1
A result = ""
B for ch in text:
C if ch.isalpha():
D n = ord(ch) - 97
E n = (n + shift) % 26
F result += chr(n + 65)
Line D — Upper-case letters start at ord('A') = 65, so subtract 65 to get a position from 0 to 25. Subtracting 97 is for lower-case letters and would give negative numbers here. It should be n = ord(ch) - 65.
Question 2
A result = ""
B for ch in range(text):
C n = ord(ch) - 65
D n = (n + shift) % 26
E result += chr(n + 65)
F print(result)
Line B — range() needs a number, not a string, so this causes a TypeError. A for loop can go straight over the characters of the message: for ch in text:.
Question 3
A def caesar(text, shift):
B result = 0
C for ch in text:
D n = ord(ch) - 65
E n = (n + shift) % 26
F result += chr(n + 65)
Line B — The ciphertext is built by adding characters, so result must start as an empty string. Starting at 0 makes += chr(...) fail because you can't add a string to a number. It should be result = "".
Question 4
A text = "HELLO"
B result == ""
C for ch in text:
D n = ord(ch) - 65 + 3
E result += chr(n % 26 + 65)
Line B — == compares two values, it does not create a variable. result is never set, so result += ... causes a NameError. It should use a single = to assign: result = "".
Question 5
A result = ""
B for ch in text:
C n = chr(ch) - 65 + shift
D n = n % 26
E result += chr(n + 65)
Line C — chr() turns a number into a letter, but ch is a letter. To get its number code you need ord(ch). It should be n = ord(ch) - 65 + shift.

Student with a class code? Join your class →