Page 217 -
P. 217

format string


            If you have just                                              SOlUTion
            one value to format,
            you don't need to
            surround it with         You were to match each format string to what it does.
            parentheses.



              "%s %e" % ("Value is", 16.0 ** 0.5)             Display a string followed by 4.000000.



              "%7d" % (11232/3)                               Display a string followed by 4.000000e+00.



              "%x" % 127                                      Display the value, padded with 0s.
            \n means take a NEWLINE.

              "%20s\n" % "Banana swirl"                       Display the result of the calculation, padded
           This means “use 4 characters."                     with spaces.         By default, Python will pad
                                                                                   using spaces.
              "%s is $%4.2f" % ("Popsicle", 1.754)            Pad the string out to 20 characters, then display a
                      This means “show 2 numbers              newline character.
                      after the decimal point."

              "%s %f" % ("Value is", 16.0 ** 0.5)             Display the number as a hexidecimal (base 16).
                               Values can be the
                                result of a calculation.
                                                              As well as a string, also display a floating point
              "%07d" % (11232/3)
                                                              number to 2 decimal places.  Hexadecimal numbers
            Following the % with a 0                                                     are used for things
            means “pad with zeroes."                                                     like colors on the Web.





                                                You were to look back at the records you need to create for the
                                                transaction file and write down what you would use for the
              Don't worry if your answer doesn't   format string:
              look EXACTLY like this. Try out your                              Multiplying by 100 and
              answer in the Python Shell to check                               displaying it as a whole number
              that it works.                                                    effectively removes the
                                                                                decimal point from the price.
                              “%16s%07d%16s\n" % (credit_card, price*100, description)
             Credit card numbers should always   You need a newline character
             be exactly 16 characters.          at the end of each line.

           182    Chapter 6
   212   213   214   215   216   217   218   219   220   221   222