Skip to main content
Chemistry LibreTexts

8.10: 3.format() method

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

    Generic Syntax

    "text {} text {} text {}." .format(var1, var2, var3)

    • Can use index numbers
    • Place width and precision in curly brackets ({}).

    Index Numbers

    #Demonstration of use of index numbers in .format() method
    atom1, atom2, atom3 = "Hydrogen", "Helium", "Lithium"
    print("This string has order first, second, third variable {} {} {}."\
          .format(atom1,atom2,atom3))
    print("This string has order third variable {2}, second variable {1} "\
          "first variable {0}.".format(atom1, atom2, atom3))
    print("This string has order first variable {0}, second variable {1} "\
          "first variable {0}.".format(atom1, atom2, atom3))
    Hello world!
    Exercise \(\PageIndex{1}\)

    What value do index numbers in Python start with (what is the index number of the first variable?

    Answer

    0, atom one is the first variable and has an index number of zero.

     

    Width and Precision

    Analyze the following code, predict the output and run.  If it is different than predicted, be sure to understand why.

    Note; we are using the pipe symbol (|) to indicate the divider between two columns in our data output. The first column is the names of elements are they are string variables, and the second are their molar masses, and they are floats

    #Demonstration of use of width and precisioin of .format() method
    print("{0:10}|{1:10}\n{2:10}|{3:10}\n1234567890|1234567890"\
          .format("hydrogen",1.00784, "helium", 4.002602))
    print(10*" * ")
    print("{0:10}|{1:10.3f}\n{2:10}|{3:10.3f}".format("hydrogen",1.00784, "helium", 4.002602))
    Hello world!
    Exercise \(\PageIndex{2}\)

    Adjust the code above so the print output of the atomic masses below the row of stars show a precision of 4 decimal places

    Answer

    print("{0:10}|{1:10.4f}\n{2:10}|{3:10.4f}".format("hydrogen",1.00784, "helium", 4.002602))

     

    Aligning columns

    Run and analyze the following code to figure out the syntax to left align, right align and center align data in a columns.   

    #left aligned spaced at 15 digits
    print("{0:<15}|{0:<15}".format("left align", "center align", "right align"))
    #left then right aligned spaced at 15 digits
    print("{0:<15}|{2:>15}".format("left align", "center align", "right align"))
    #center aligned spaced at 15 digits
    print("{1:^15}|{1:^15}".format("left align", "center align", "right align"))
    Hello world!

     

     


    This page titled 8.10: 3.format() method is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?