1: Formatting
- Page ID
- 206752
\( \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}\)Formatting in Python 2.5 and earlier
Until Python 2.6 formatting was done using % operator. We will not be using this method, but it is important to understand it in case you will have to work with code written before the changes.
Operator | Used for |
---|---|
%s | String (or any object with a string representation, like numbers) |
%d | Integers |
%f | Floating point numbers |
%.<number of digits>f | Floating point numbers with a fixed amount of digits to the right of the dot |
%x/%X | Integers in hex representation (lowercase/uppercase) |
Formatting using string .format() method (Python 2.6 and up)
To use this method you need to put placeholders that look like a pair of curvy brackets {} in your string. After the string you need to add .format () and indicate the value(s) you want to use instead of the curvy brackets.
Here is an example:
Note, that the variables in parentheses are not in the same order they are used in the print statement. We are using positional parameter to indicate which variable goes where. One thing to remember here is that in Python numbering starts with zero. In our example atomic_mass is #0 and element is #1. You can also use other parameters to further customize the output (see Formatting Parameters section).
The following script calculates the pH of a random acid and its salt (base) concentration if you give it the Ka of the acid. Note the commented out code uses the modules format while the code that is working uses the format function.
Formatting using f.strings (Python 3.6 and up)
The newest way of formatting is to use a so-called f-string. This method is very similar to the .format(), but instead of specifying the values after the string we can include them in the curly brackets {} in the string itself. You need to add “f” at the beginning of your string to let Python know that you are using this method. See an example below:
Formatting Parameters
Sometimes you might want to see your output in a certain way (number of digits, aligned right, centered, etc.). Starting with Python 2.6 we can add multiple options to the string to get the output we need. For example, if we only want to see three significant digits we can use the f.string method with some modifications:
You can see that Python automatically rounds the number. Let's look at some other parameters we can use in the similar way.
Option | Meaning |
---|---|
< | Left-align within the available space (default for strings) |
> | Right-align within the available space |
0 | If the width field is preceded by a zero ('0') character, sign-aware zero-padding for numeric types will be enabled |
, | Indicates using commas for a thousands separator |
= | Forces the padding to be placed after the sign (if any) but before the digits. Only valid for numeric types |
^ | Center within the available space |
+ | Use a sign for positive and negative numbers |
- | Use a sign for negative numbers only |
space | Use leading space for positive numbers, and a minus sign for negative numbers |
You can find more information and other common string operators here.
______________
Format Function
there appear to be two ways to use the format function
print("Total salary: ${:.2f}".format(25))
print("Total salary: $",format(salary,'0.2f'))
The first allows you to format more than one thing
print("Total salary: ${:.2f} for {:d} days".format(25,32))
A variable can be a number, string... and there are different ways to format
fill - any character can be filled to a given number|
sign - This could be a +, - or blank space
align - The alignment could be <, >, ^ or =
precision - An integer that tells how precisely the value passed to format method has to be formatted.
type - This specifies the type - binary, octal, exponential, etc. The other types will be listed in the upcoming post, as and when required.
width - An integer value specifying how wide the value has to be formatted to.
Resources:
https://pyformat.info/#annotations:al8dbkiAEeqQq_f9ta6urw