Python Program to Implement Caesar Cipher
chr((ord(char) - base + shift) % 26 + base) for letters; for example, def caesar_cipher(text, shift): return ''.join(chr((ord(c) - base + shift) % 26 + base) if c.isalpha() else c for c in text for base in [ord('A') if c.isupper() else ord('a')] if c.isalpha()).Examples
How to Think About It
%. Non-letter characters stay the same. Then convert the number back to a letter.Algorithm
Code
def caesar_cipher(text, shift): result = [] for char in text: if char.isalpha(): base = ord('A') if char.isupper() else ord('a') shifted = (ord(char) - base + shift) % 26 + base result.append(chr(shifted)) else: result.append(char) return ''.join(result) print(caesar_cipher('Hello, World!', 3))
Dry Run
Let's trace 'Hello, World!' with shift 3 through the code
Check first character 'H'
'H' is uppercase, base = 65, ord('H')=72, shifted = (72-65+3)%26+65=75, chr(75)='K'
Check second character 'e'
'e' is lowercase, base = 97, ord('e')=101, shifted = (101-97+3)%26+97=104, chr(104)='h'
Check character ','
Not a letter, keep ',' as is
Continue for all characters
Apply same logic for each letter, keep punctuation unchanged
| Character | IsAlpha | Base | Ord | Shifted Ord | Result Char |
|---|---|---|---|---|---|
| H | True | 65 | 72 | 75 | K |
| e | True | 97 | 101 | 104 | h |
| l | True | 97 | 108 | 111 | o |
| l | True | 97 | 108 | 111 | o |
| o | True | 97 | 111 | 114 | r |
| , | False | - | - | - | , |
| False | - | - | - | ||
| W | True | 65 | 87 | 90 | Z |
| o | True | 97 | 111 | 114 | r |
| r | True | 97 | 114 | 117 | u |
| l | True | 97 | 108 | 111 | o |
| d | True | 97 | 100 | 103 | g |
| ! | False | - | - | - | ! |
Why This Works
Step 1: Identify letters and base
We check if a character is a letter and find its base ASCII code: 65 for uppercase, 97 for lowercase.
Step 2: Shift letters with wrap-around
We convert the letter to a number 0-25, add the shift, then use modulo 26 to wrap around if needed.
Step 3: Convert back to character
We add the base back to the shifted number and convert it to a character to get the encrypted letter.
Step 4: Keep non-letters unchanged
Characters that are not letters, like spaces or punctuation, are added to the result without change.
Alternative Approaches
def caesar_cipher(text, shift): import string lower = string.ascii_lowercase upper = string.ascii_uppercase shifted_lower = lower[shift:] + lower[:shift] shifted_upper = upper[shift:] + upper[:shift] trans = str.maketrans(lower + upper, shifted_lower + shifted_upper) return text.translate(trans) print(caesar_cipher('Hello, World!', 3))
def caesar_cipher(text, shift): return ''.join( chr((ord(c) - base + shift) % 26 + base) if c.isalpha() else c for c in text for base in [ord('A') if c.isupper() else ord('a')] if c.isalpha() ) print(caesar_cipher('Hello, World!', 3))
Complexity: O(n) time, O(n) space
Time Complexity
The program processes each character once, so time grows linearly with input size.
Space Complexity
It creates a new string of the same length, so space grows linearly with input size.
Which Approach is Fastest?
Using string translation with maketrans is generally faster and more concise than manual character shifting.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Manual shifting with ord/chr | O(n) | O(n) | Learning and flexibility |
| String translation with maketrans | O(n) | O(n) | Performance and simplicity |
| List comprehension | O(n) | O(n) | Compact code but less readable |