Implementing a Caesar Cipher
00:00 The Caesar cipher is an encryption technique named after the Roman general Julius Caesar, who reportedly used it to encrypt military messages. The cipher is pretty straightforward.
00:13 The idea is that all of the letters of the alphabet are shifted by some fixed amount. So for example, if we’re going to be shifting all letters by 4, we take the standard alphabet in the natural order and then the letter A, if we shift by 4 becomes the letter E, B becomes the letter F, C becomes G, and so on.
00:39 When we get to W, we have to then wrap around the alphabet, so W becomes A, X becomes B, and then Y and Z become C and D.
00:51 If a character in the string is not part of the alphabet, then we don’t transform it and we just leave it as it is—so, things like punctuation, whitespace, and other non-alphabet characters.
01:04 If you think about what this is going to be doing to a string, which is an iterable, we’re going to be mapping this function that takes A to E, B to F, C to G, and so on at each character of the string.
01:18 So again, we have this idea of wanting to map a function at every element of an iterable to return a new transformed iterable. All right, let’s write this up and give it a try.
Before we go ahead and write the cipher, we’re going to be using a module called the
string module, which contains as one long string all of the letters of the alphabet in lowercase.
We’re going to import from the
string module, we’re going to import all of the ASCII lowercase characters, and we’re going to import this and call it the
alphabet, and I’ll show you in a second what the
alphabet is going to be.
So go ahead and type
alphabet, and the
alphabet just contains all of the lowercase letters of the alphabet. We’re going to use that in our cipher, and you could import this right into the function. That way, if you send this function to somebody else, it’s all self-contained.
02:17 But in this case, we’ll just import it like this.
All right, so now let’s go ahead and define the cipher. We’ll call it
caesar(), so Caesar cipher, and it’s going to take just a character, let’s say
02:32 And we’re going to define in the function the amount that we want to shift by. Now, this may be a parameter that instead you want to pass into the function, but just for illustration purposes, let’s just define it right in the function.
So let’s just say we want to shift by
3. Now, because we’re only interested in the lowercase characters, we want to make sure that if the message comes in with uppercase letters, we want to first convert the input character into lowercase, so we’ll call the
.lower() method in the
And then the first thing that we’ll do is check to see if the character is not in the
alphabet, and if it’s not, then we just want to simply return the character.
03:18 So any type of punctuation or non-alphabet characters, we just want to simply return them.
The first thing that we want to do is find the index of the character in the
alphabet, so we’ll call the
.index() method on the
This is going to be an integer between
alphabet is a string object, which is an iterable, and the index starts at
0 and the number of letters in the
26, so this will be an integer from
Then we need to shift the index by the shift amount. This might take us over
25, and so what we need to do is use the modulo operator (
%) so that we get the wrapping done correctly.
The number of letters in the alphabet is 26 and we can just hard-code that in, but maybe to make it a little bit more programmatic, we’ll just call the
len() (length) function on the
alphabet. That way, if you are using a different alphabet, you’re not hard-coding in
26 and you’re just using the length of the
alphabet that you’re using.
So, why don’t we call this, say, the shifted index or
04:34 and then we simply want to return the character, or the letter, in the alphabet that corresponds to the shifted index. Go ahead and run that. And why don’t we give this a try?
So, for example, if we take the letter
'a' and we’re shifting it by
3, that’ll give us
'd'. If we shift the letter
'b', that should give us
'e'. Now, the letter
'x' is going to be the first letter that wraps around the alphabet, so
'x' will get mapped to
'b', and then
'c'. All right, so why don’t we give this a try with a string?
Let’s clear that out and let’s map our Caesar cipher to an iterable, which is going to be a string, and maybe this is the secret message that we want to encode and so why don’t we write
"My secret message goes here". And we know we’re going to get an iterator.
And why don’t we just go ahead and save this as, say, the
Now let’s see what the
secret is after the encryption. Remember that the
secret variable is an iterator—it’s going to return each encrypted character one at a time.
So if we want to see this as a string, let’s join the elements that are returned by the
secret iterator with an empty string. And this’ll be okay because remember, the blank spaces in the original string are kept intact, so we’ll have the whitespaces already built in to the encrypted string.
This seems to be working. For example, the letter
'm' if we shift it by 3 characters, the lowercase letter
'm', we’re going to get
And the letter
'y' if we shift it by 3 characters, we’re going to get
'b'. And so that seems to be working well.
06:36 All right! So this is, again, another good example of this general idea of wanting to map a function at every element of an iterable to return a new iterable that yields the transformed characters.
All right, this wraps up the lesson. In the next lesson and the one after that, we’re going to take a look at how we can combine the
map() function with two other important functions in functional programming.
Become a Member to join the conversation.