Skip to main content
Chemistry LibreTexts

8.8: Print Formatting Overview

  • Page ID
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    We are calling this print formatting, but really we are dealing with different methods for formatting the output of data, and different data types have different issues.  For example, only a float has a decimal where you need to be concerned about how many digits after the decimal are formed.  So the issues with integers are different than strings which are different than floats.

    We will cover three formatting methods, in the order that they were used.  I recommend students focus on the f-string method for code they write, as that is the simplest.  But you may find code, especially older python 2 code that uses the older methods, and you need to be able to analyze and figure how it works.  

    Three Print Formatting Methods

    1. Modulo (%) method
      • oldest and may be in packages you import
    2. .format() method
      • set up like a function
    3. f-string method
      • by far the easiest to use

    It should be noted that all formatting methods are doing the same thing, they are formatting data output, and so they are all dealing with the same issues.  Therefor it is a idea to quickly look over each method to get a bearing on the issues (and opportunities) you can encounter, and then choose the method that is best for your needs.  In most cases, you will use the f-string method in original code you write.  But if you borrow code that has used other methods, you might as well continue to use them, and so it is good to be familiar with them.

    This page titled 8.8: Print Formatting Overview is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?