Implementing the Caesar cipher in Python

Let's go ahead and open the Terminal and follow these steps to implement Caesar cipher in Python:

  1. We will use Python in interactive mode first and then make a string that just has some letters in order to test this method:
>>> str = "ABCDE">>> str.find("A")0>>> str.find("B")1>>> exit()
  1. Because we understand how the string methods work, we'll exit and go into the nano text editor to look at the first version of our script:
$ nano caesar1.py
  1. When you run the command, you will get the following code:
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"str_in = raw_input("Enter message, like HELLO: ")n = len(str_in)str_out = ""for i in range(n):   c = str_in[i]   loc = alpha.find(c)   print i, c, loc,  newloc = loc + ...

Get Hands-On Cryptography with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.