Hacking Secret Ciphers with Python

(Ann) #1

118 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Once the for loop has finished looping for the rest of the columns, the value in ciphertext
will be ['Ceno', 'onom', 'mstm', 'me o', 'o sn', 'nio.', ' s ', 's c'].
We will use the join() string method to convert this list of strings into a single string.


The join() String Method


The join() method is used later on line 39. The join() method takes a list of strings and
returns a single string. This single string has all of the strings in the list concatenated (that is,
joined) together. The string that the join() method gets called on will be placed in between the
strings in the list. (Most of the time, we will just use a blank string for this.) Try typing the
following into the interactive shell:





eggs = ['dogs', 'cats', 'moose']
''.join(eggs)
'dogscatsmoose'
' '.join(eggs)
'dogs cats moose'
'XYZ'.join(eggs)
'dogsXYZcatsXYZmoose'
''.join(eggs).upper().join(eggs)
'dogsDOGSCATSMOOSEcatsDOGSCATSMOOSEmoose'





That last expression, ''.join(eggs).upper().join(eggs), looks a little tricky, but if
you go through the evaluation one step at a time, it will look like this:


Figure 8- 4. The steps of evaluation for ''.join(eggs).upper().join(eggs)
Free download pdf