Skip to main content
Practice

Caesar Cipher - Problem Solving

Discover two methods to implement Caesar Cipher in Python.


Method 1
def solution(text, shift):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
result = ''

for char in text:
if char.isalpha():
index = alphabet.index(char.lower())
shifted_index = (index + shift) % 26
shifted_char = alphabet[shifted_index]
result += shifted_char.upper() if char.isupper() else shifted_char
else:
result += char

return result

Example Usage

Input/Output Example
result = solution("abc", 3)

print(result) # Output: "def"

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.