Editor

Caesar Cipher

#34 · Strings · Beginner · 35 XP · about 8 min

Julius Caesar is said to have hidden his messages by moving every letter a fixed number of places along the alphabet. Write caesar_shift(text, shift) that returns text with every letter moved shift places forward, wrapping around from z back to a.

Uppercase letters stay uppercase and lowercase letters stay lowercase. Anything that is not a letter (spaces, digits, punctuation) is copied unchanged. A negative shift moves letters backwards, which is how you decode a message.

Examples

caesar_shift("Hello, World!", 3)  # returns 'Khoor, Zruog!'
caesar_shift("xyz", 2)            # returns 'zab'
caesar_shift("Khoor", -3)         # returns 'Hello'

In the second example x moves to z, y wraps around to a, and z wraps around to b.

Note

shift can be any whole number, even one larger than 26 or smaller than -26: a shift of 29 does the same as a shift of 3. The text contains plain ASCII characters only.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.