Editor

Caesar Cipher for Any Text

Intermediate · Write the code · from Safe Computing: Privacy, Passwords, Encryption and Phishing

From the lesson: Safe Computing: Privacy, Passwords, Encryption and Phishing

Your phone, your browser and your apps collect a steady stream of data about you, and the same data that makes them convenient can be used to track you, trick you or steal from you.

Read the lesson

Write caesar_encrypt(text, key) that shifts every letter of text by key places. Unlike the demo, it must handle both cases:

  • Uppercase letters stay uppercase and lowercase letters stay lowercase.
  • Spaces, digits and punctuation stay as they are.
  • key can be negative (to decrypt) or larger than 26.
  • caesar_encrypt("Meet at 9!", 1)  ->  "Nffu bu 9!"
    UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    LOWER = "abcdefghijklmnopqrstuvwxyz"
    def caesar_encrypt(text, key):
    result = ""
    # Shift each letter, keep everything else
    return result

    What Check runs

    Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

    RunsExpected output
    print(caesar_encrypt("HELLO", 3))KHOOR
    print(caesar_encrypt("xyz", 3))abc
    print(caesar_encrypt("Meet at 9!", 1))Nffu bu 9!

    More Safe Computing exercises

    All Python exercises · 50 practice problems