Caesar Cipher in Python
Shift every letter three places: code becomes frgh. Learn ord(), chr() and the % trick that wraps z back to a.
- Character codes with ord() and chr()
- Wrapping around the alphabet with % 26
- Building the encrypted string
cfordgehShift and wrap
The problem
The Caesar cipher is one of the oldest ways to hide a message, and it is a perfect exercise in turning letters into numbers, doing arithmetic on them and turning them back. The tricky part is what happens after z.
Think of it like this
A Caesar cipher works like a combination lock wheel with the alphabet on it. Turning the wheel three clicks moves every letter three places forward. When you pass z, the wheel simply continues at a, because it is a circle.
ord(character) - ord("a")🔤 Find the letter's place on the wheel: 0 to 25+ shift🔄 Turn the wheel three clicks% 26⭕ Past z, continue at achr(... + ord("a"))🔡 Read the new letterA Caesar cipher replaces each letter with the letter a fixed number of places further along the alphabet, wrapping from z back to a.
Key points
ord(character) - ord("a")turns a letter into its position, from 0 for a to 25 for z.+ shiftmoves the position three places forward.% 26wraps positions past 25 back to the start of the alphabet.chr(... + ord("a"))turns the position back into a letter, so"code"becomes"frgh".
When should you use it?
- Learning how characters map to numbers with
ord()andchr(). - Practicing modulo for values that wrap around, like clocks and days of the week.
- Simple puzzles and escape-room style games.
When not to use it
- Protecting real data: a Caesar cipher can be broken by trying all 25 shifts, so use real encryption libraries.
- Text with capitals, spaces or punctuation: this version only handles lowercase letters and would scramble other characters.
Example code
message = "code"
shift = 3
# Shift each lowercase letter and wrap after z.
encrypted = "".join(
chr((ord(character) - ord("a") + shift) % 26 + ord("a"))
for character in message
)
print(encrypted)
Result of running the code
frgh
Code explanation
ord(character) - ord("a") turns each letter into a number from 0 to 25. Adding shift moves it forward, and % 26 wraps any result past 25 back to the start, so x, y and z become a, b and c. chr(... + ord("a")) converts the number back into a letter, and "".join puts the letters together into frgh.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Forgetting to wrap around
Letters near the end of the alphabet turn into symbols.
chr(ord(character) + shift) # "z" becomes "}"
chr((ord(character) - ord("a") + shift) % 26 + ord("a"))
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- DNA to RNA Transcription Build an RNA string by replacing every DNA character with its partner. Easy