Hacking Secret Ciphers with Python

(Ann) #1

162 http://inventwithpython.com/hacking


Email questions to the author: [email protected]








On line 23, if the user did not type in 'c', 'continue', 'C', or another string that begins
with C, then sys.exit() will be called to end the program. Technically, the user doesn’t have
to enter “Q” to quit; any string that does not begin with “C” will cause the sys.exit() function to
be called to quit the program.


There is also an endswith() string method that can be used to check if a string value ends with
another certain string value. Try typing the following into the interactive shell:





'Hello world!'.endswith('world!')
True
'Hello world!'.endswith('world')
False





The title() String Method


Just like the lower() and upper() string methods will return a string in lowercase or
uppercase, the title() string method returns a string in “title case”. Title case is where every
word is uppercase for the first character and lowercase for the rest of the characters. Try typing
the following into the interactive shell:





'hello'.title()
'Hello'
'HELLO'.title()
'Hello'
'hElLo'.title()
'Hello'
'hello world! HOW ARE YOU?'.title()
'Hello World! How Are You?'
'extra! extra! man bites shark!'.title()
'Extra! Extra! Man Bites Shark!'





transpositionFileCipher.py



  1. Read in the message from the input file



  2. fileObj = open(inputFilename)

  3. content = fileObj.read()

  4. fileObj.close()



  5. print('%sing...' % (myMode.title()))

Free download pdf