Cesar Cipher¶
Photo by Christian Lendl on unsplash.com
In this chapter you will:¶
area |
topic |
|---|---|
🚀 |
encrypt and decrypt text |
⚙ |
index the positions of a string |
⚙ |
loop over a string |
💡 |
use the |
🔀 |
build a longer string by concatenation |
🔀 |
use the same index for two strings |
🐞 |
fix index errors |
Exercise 1: Cesar Cipher¶
Execute the following code calculating a Cesar Cipher:
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
for i in range(26):
print(plain[i], " -> ", encrypted[i])
Explain what the code does.
Exercise 2: String methods¶
Find out what the expressions do to the string in the middle.
Exercise 3: Loop over a string¶
The following program should print the position in the alphabet of every character.
Complete program by inserting char, message, plain and position:
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
message = "MY SECRET MESSAGE"
for char in ___:
position = ___.find(___)
print(char, "is in position", ___)
Why is the space at the end of plain important?
Exercise 4: String concatenation¶
Explain the following code:
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
s = ""
s += encrypted[4]
s += encrypted[1]
s += encrypted[8]
s += encrypted[8]
s += encrypted[11]
print(s)
Exercise 5: Encryption¶
Write a program that:
defines a plain and encrypted alphabet as 26-character strings
reads a message from the keyboard
defines an empty result string
goes through each character of the message
finds the position in the plain text alphabet
looks up that position in the encrypted alphabet
adds the encrypted character to the result string
when all characters have been processed, output the result
Hint
After each step, you should be able to run the program and see what it already does.
Exercise 6¶
Explain why the following code does the same as in exercise 1:
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(26):
print(plain[i], " -> ", plain[(i + 3) % 26])
Can you use it to make your encryption program shorter?
Hint
If you are not sure what happens, print the value of i inside the loop.
Exercise 7¶
Also write a program for decryption.
Hint
How could you make sure that the input string is in upper case?
Reflection questions¶
what happens when a string is followed by square brackets?
describe two ways to loop over the characters of a string?
what does the
str.find()method do?how can you add characters to a string?