easy +8 pts

Caesar Cipher Shift

Implement a classic Caesar cipher to encrypt a string with a given shift.

Implement the function `caesar_cipher(text: str, shift: int) -> str` that returns a new string where each alphabetic character is shifted by `shift` positions in the alphabet. The shift wraps around from 'z' to 'a' and 'Z' to 'A'. Non-alphabetic characters (numbers, spaces, punctuation) remain unchanged. Uppercase and lowercase letters are preserved separately. The shift can be any integer (including negative or large).

Constraints

Input length of `text` is between 0 and 1000. `shift` is an integer in the range -1000 to 1000. Use only ASCII letters.

Example

>>> caesar_cipher('abc', 3)
'def'
>>> caesar_cipher('xyz', 3)
'abc'
>>> caesar_cipher('Hello, World!', 5)
'Mjqqt, Btwqi!'
>>> caesar_cipher('abc', -1)
'zab'
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `str.isalpha()` to check for letters.
Use `ord()` and `chr()` to convert between characters and ASCII codes.
For each letter, compute the base (ord('a') or ord('A')) and apply the shift modulo 26.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.