Hacking Secret Ciphers with Python

(Ann) #1

76 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Because the upper() method returns a string value, you can call a method on that string as
well. Try typing 'Hello world!'.upper().lower() into the shell:





'Hello world!'.upper().lower()
'hello world!'





'Hello world!'.upper() evaluates to the string 'HELLO WORLD!', and then we call
the lower() method on that string. This returns the string 'hello world!', which is the
final value in the evaluation. The order is important. 'Hello world!'.lower().upper()
is not the same as 'Hello world!'.upper().lower():





'Hello world'.lower().upper()
'HELLO WORLD!'





If a string is stored in a variable, you can call any string method (such as upper() or
lower()) on that variable. Look at this example:





fizz = 'Hello world!'
fizz.upper()
'HELLO WORLD!'
fizz
'Hello world!'





Calling the upper() or lower() method on a string value in a variable does not change the
value inside a variable. Methods are just part of expressions that evaluate to a value. (Think about
it like this: the expression fizz + 'ABC' would not change the string stored in fizz to have
'ABC' concatenated to the end of it, unless we used it in an assignment statement like fizz =
fizz + 'ABC'.)


Different data types have different methods. You will learn about other methods as you read this
book. A list of common string methods is at http://invpy.com/stringmethods.


The for Loop Statement


caesarCipher.py



  1. run the encryption/decryption code on each symbol in the message string



  2. for symbol in message:

Free download pdf