The on-line python documentation for the complete set of formatting codes can be found at http://docs.python.org/library/stdtypes.html#string-formatting-operations and goes further than this quick document. We have tried to select the most useful codes here. Strings We consider the string “Hello, world!”. Formatting code %s %20s %-20s %3s Hello, world! 'Hello, world' ' Hello, world!' 'Hello, world! ' 'Hello, world'
Integers We consider the integers 12,345 and -12,345. Formatting code %d %20d %-20d %020d %+d %+20d %+-20d %+020d %3d 12,345 '12345' ' 12345' '12345 ' '00000000000000012345' '+12345' ' +12345' '+12345 ' '+0000000000000012345' '12345' -12,345 '-12345' ' -12345' '-12345 ' '-0000000000000012345' '-12345' ' -12345' '-12345 ' '-0000000000000012345' '-12345'
Floating point numbers We consider the floating point numbers 12·34567 and -12·34. The %f formatting code presents data in decimal notation. The %e code does it in exponential form. Formatting code %f %20f %-20f %020f %+f %+20f %+-20f %+020f %.4f %20.4f %-20.4f %020.4f %+.4f %+20.4f %+-20.4f %+020.4f Formatting code %e %20e %-20e %020e %+e %+20e %+-20e %+020e %.4e %20.4e %-20.4e %020.4e %+.4e %+20.4e %+-20.4e %+020.4e 12·34567 '12.345670' ' 12.345670' '12.345670 ' '0000000000012.345670' '+12.345670' ' +12.345670' '+12.345670 ' '+000000000012.345670' '12.3457' ' 12.3457' '12.3457 ' '000000000000012.3457' '+12.3457' ' +12.3457' '+12.3457 ' '+00000000000012.3457' 12·34567 '1.234567e+01' ' 1.234567e+01' '1.234567e+01 ' '000000001.234567e+01' '+1.234567e+01' ' +1.234567e+01' '+1.234567e+01 ' '+00000001.234567e+01' '1.2346e+01' ' 1.2346e+01' '1.2346e+01 ' '00000000001.2346e+01' '+1.2346e+01' ' +1.2346e+01' '+1.2346e+01 ' '+0000000001.2346e+01' -12·34 '-12.340000' ' -12.340000' '-12.340000 ' '-000000000012.340000' '-12.340000' ' -12.340000' '-12.340000 ' '-000000000012.340000' '-12.3400' ' -12.3400' '-12.3400 ' '-00000000000012.3400' '-12.3400' ' -12.3400' '-12.3400 '