Computer Studies‎ > ‎Python‎ > ‎References‎ > ‎

Cypher Shift

Intro Video

Caesar Shift Cypher

Example Program


'''
This program encrypts uppercase characters by shifting them two the right. A becomes C, B becomes D, etc.

Note the use of modulo to handle the end of the alphabet, i.e., Y becomes A, Z becomes B
'''

import string

# number of characters to shift over
shift = 2

# print the string being encrypted
print(string.ascii_uppercase)

# print each shifted character
for letter in string.ascii_uppercase:
    print(chr((ord(letter) - 65 + shift) % 26 + 65), end='')

Questions

  1. Why have we subtracted 65 from the ordinal value of each character?
  2. Why have used modulo 26?

Assignment 1

Modify the program above to work exclusively with lowercase letters.

Assignment 2

Modify the program(s) above to work with both upper- and lowercase letters.
Comments