4: f-string formatting
- Page ID
- 431770
\( \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.
Exercise \(\PageIndex{15.a}\)
Answer the following questions concerning the above code
- Which variable is a string?
- Which variable is an integer?
- Which variables are floats?
- What does \ do?
- What does \n do?
- 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
Exercise \(\PageIndex{1}\)
consider the expression {variable_name:xx.yyf}
- What does the colon indiate?
- What does the xx indicate?
- What does the yy indicate?
- 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
- The :2.3f has a width of 2 and a precision of 3, so how long will the number be printed out?
- Which variable is an integer?
- Which variables are floats?
- What does \ do?
- What does \n do?
- 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