Skip to main content
Chemistry LibreTexts

8.11: f-string formatting

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    f-strings are the most recent and easiest way to format output, and so we will cover them first.  You simply place an f in front of the quotes, and use curly brackets to insert variable names.

    • Introduced in Python 3.6
    • Place f in front of string
    • allows you to skip the .format() step

     Analyze the code, then run it, and then answer the questions below it.

    molecule ="hydrogen"
    molarmass=1.00794
    mass = 10
    moles =mass/molarmass
    print(f'{mass} grams of the molecule {molecule} has {moles} moles.\n \t{mass} \
    grams of the molecule {molecule} has {moles} moles. \n \
    \t\t{mass} grams of the molecule {molecule} has {moles} moles.\n \
    \t\t\t the molar mass of hydrogen is {molarmass}.\n \
    This is the last line.')
    Hello world!

     

    Exercise \(\PageIndex{15.a}\)

    Answer the following questions concerning the above code

    1. Which variable is a string?
    2. Which variable is an integer?
    3. Which variables are floats?
    4. What does \ do?
    5. What does \n do?
    6. What does \t do?
    Answer a
    molecule
    Answer b
    mass
    Answer c
    molar mass and moles, which is an integer divided by a float
    Answer d
    Allows you to extend a long line of python script to the next line (this f-string is very long).  The best practice is not to have a line of code longer than 80 characters.
    Answer e
    Next line (like the carriage return on a typewritter)
    Answer f
    tab

     

    Note that we often want to limit the number of digits displayed to the right of the decimal to represent the precision with which a number is known. In the following example we will try and show how this is done for f-strings.  we first print 20 decimal digits, so you can see what position a number is printed, and then we print the variable twice, using different format conventions

    variable=12345.6789
    print(f'12345678901234567890')
    print(f'{variable:.4f}\n{variable:.2f}')
    
    print(f'12345678901234567890')
    print(f'{variable:10.2f}\n{variable:11.2f} \n{variable:11.3f}')
    
    Hello world!

     

    Exercise \(\PageIndex{1}\)

    consider the expression  {variable_name:xx.yyf}

    1. What does the colon indiate?
    2. What does the xx indicate?
    3. What does the yy indicate?
    4. What does the f indicate?
    Answer a

    The colon indicates the following statement describes the formatting of the variable in the print statement

    Answer b

    This tells you the width of the number.  This allows you to right justify all numbers.

    Answer c

    This tells you the precision of the number, that is, how many values to the right of the decimal are expressed in the print statement

    Answer d

    this is a floating decimal number

     

    Exercise \(\PageIndex{15.a}\)

    Consider the following code and answer question (a) before running it

    print(f"How long is this number going to be {1234.5678:2.3f}")
    Hello world!

     

    1. The :2.3f has a width of 2 and a precision of 3, so how long will the number be printed out?
    2. Which variable is an integer?
    3. Which variables are floats?
    4. What does \ do?
    5. What does \n do?
    6. What does \t do?
    Answer a
    The entire number will be printed to the third decimal, so 8 characters including the dot.
    Answer b
    mass
    Answer c
    molar mass and moles, which is an integer divided by a float
    Answer d
    Allows you to extend a long line of python script to the next line (this f-string is very long).  The best practice is not to have a line of code longer than 80 characters.
    Answer e
    Next line (like the carriage return on a typewritter)
    Answer f
    tab

     


    This page titled 8.11: f-string formatting is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?