Beginner Python Exercises

Caesar Cipher in Python

Shift every letter three places: code becomes frgh. Learn ord(), chr() and the % trick that wraps z back to a.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Character codes with ord() and chr()
  • Wrapping around the alphabet with % 26
  • Building the encrypted string
shift = 3
cf
or
dg
eh
x, y, z wrap around to a, b, c.

Shift 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.

From an alphabet wheel to code
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 a
chr(... + ord("a"))🔡 Read the new letter
Letters become numbers, move, wrap around and become letters again.
In one sentence

A 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

  1. ord(character) - ord("a") turns a letter into its position, from 0 for a to 25 for z.
  2. + shift moves the position three places forward.
  3. % 26 wraps positions past 25 back to the start of the alphabet.
  4. 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() and chr().
  • 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

Run code →
main.py
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)
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Forgetting to wrap around

Letters near the end of the alphabet turn into symbols.

Problem
chr(ord(character) + shift)  # "z" becomes "}"
Fix
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.

All challenges