Skip to content

Project 4: Caesar cipher

In his secret letters, Julius Caesar would shift each letter a few steps along the alphabet: a → b, b → c… An enemy couldn’t read it, but friends knew the secret. This is one of history’s first ciphers — and you’re about to write it.

Keep the alphabet as text: alphabet = "abcdefghijklmnopqrstuvwxyz". .index() finds each letter’s position, and +1 shifts it:

  • alphabet.index("s")18, so its cipher is alphabet[19]"t"
  • The trap: shifting "z" (index 25) gives 26 — there’s no such index! The fix: % 26 closes the circle: 26 % 26 = 0"a". (Module 01’s % becomes the hero here!)
  1. Build the alphabet text
  2. Get the word with input(), start with an empty cipher = ""
  3. for letter in word: — for each letter:
    • pos = alphabet.index(letter)
    • cipher = cipher + alphabet[(pos + 1) % 26]
  4. Print the cipher
Mission

Caesar cipher

+30 XP

Follow the plan and finish the encoder. (The input is hello — its cipher should be ifmmp.) Then send a friend a secret message, and figure out the decoding rule too: just shift in the opposite direction. 🕵️

Inputs (for input())

One value per line — read in order by input().

Output
 

🏆 Congratulations — you finished the course! You wrote four real projects and earned the “Python Explorer” badge. What’s next? Write your own idea — that’s the best way to learn. If you have questions, get in touch. Good luck, programmer! 🐍