Skip to main content
Chemistry LibreTexts

3a.9: f-string formatting

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

    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 3a.9: f-string formatting is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?