### # caesar.py # # author: Kristina Striegnitz # # version: 1/27/2010 # ### def caesar_encipher(instring, num): """ The given string is encoded by rotating the letters by positions in the alphabet. For example, if is 2, then 'a' becomes 'c', 'b' becomes 'd', 'z' becomes 'b', and so on. Capitalization is preserved. All non-alphabetic characters - numbers, punctuation - are also left as they are. """ outstring = "" for inchar in instring: if inchar < "A" or inchar > "z": outchar = inchar else: incode = ord(inchar) if incode >= ord("a"): offset = ord("a") else: offset = ord("A") outcode = ((incode - offset + num) % 26) + offset outchar = chr(outcode) outstring += outchar return outstring