Skip to main content
Engineering LibreTexts

10.4: Addendum – Pretty Print

  • Page ID
    26361
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    One potential problem when printing tables is that the columns may not line up even if tabs are used for alignment. This is caused by the fact that some numbers will appear “nice”, that is, contain few digits, and others won’t. For example, one element in a column might be 25.0 while the item beneath it might be 24.83259074. The second value takes up more space and pushes the remaining items in that row over to the right thus causing a misalignment with the row above. If the values are limited to a fairly narrow range this problem can be alleviated through the judicious use of the round() function. This will not work, however, if the range of values is very wide: With large values there will still be a large number of digits and very small values may lose precision (in fact, “over rounding” may result in just zeroes being printed).

    To get around this, Python uses format specifiers. These indicate the number of digits to be used on a print out (values will be rounded, not truncated). The most useful format for scientific numeric data is of the form “%.3e”. The 3 indicates the number of digits to be displayed while the e indicates that exponent form should be used (i.e., scientific notation). An f can be used instead of e which specifies ordinary (non scientific) form or a g may be used which indicates that the shorter of e or f will be used (e.g., a small value such as 12.3 will be printed as 12.3 instead of 1.23e+01 but a very large or small value will be printed using exponent form, such as 1.23e+09 instead of 1230000000.0). The specifier is placed before the variable to be printed, separated by a percent sign. For example, to print out the variable x to 4 places using the more compact of the ordinary and exponent formats:

    print( “%.4g” % x )
    

    Note that the second percent sign is not interpreted as the modulo operator in this case. Python “knows” this by context: It simply doesn’t make sense to perform a modulo operation between a string and a number.

    Several specifiers may be used on one line. The following prints the variables x, y and z to 4 places each using exponent format and separates them with tabs:

    print( “%.4e” % x, “\t %.4e” % y, “\t %.4e” % z )
    

    The individual specifiers may be combined into one with the variables joined together with parentheses (this is called a tuple, a data type that will be examined in a later exercise):

    print( “%.4e \t %.4e \t %.4e” % (x,y,z) )
    

    Finally, be forewarned that the interpretation of “number of places” is slightly different between formats e, f and g.


    This page titled 10.4: Addendum – Pretty Print is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?