\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Brian Allan (BrianAllan)} \pdfinfo{ /Title (python-f-strings-number-formatting.pdf) /Creator (Cheatography) /Author (Brian Allan (BrianAllan)) /Subject (Python F-Strings Number Formatting Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{751AA3} \definecolor{LightBackground}{HTML}{F6F0F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python F-Strings Number Formatting Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Brian Allan (BrianAllan)} via \textcolor{DarkBackground}{\uline{cheatography.com/133708/cs/27382/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Brian Allan (BrianAllan) \\ \uline{cheatography.com/brianallan} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 16th March, 2022.\\ Updated 16th March, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{F-String Template}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{`f" text \{replacement\_field\} text ... "`}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\} Inside the quotes the f-string consists of two kinds of parts: (1) regular string literals, i.e., {\bf{text}}, and (2) {\bf{replacement fields}} containing Python expressions for evaluation along with formatting control. \newline \{\{fa-caret-right\}\} Double quotes are used here but single or triple quotes could also be used. \newline \{\{fa-caret-right\}\} F-strings may consist of just a replacement field: \newline ~~~~~{\bf{`f"\{replacement\_field\}"`}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Replacement Field}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{`\{f-expression = !conversion:format\_specifier\}`}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\} A replacement field is signaled by a pair of curly braces: `\{ \}` \newline \{\{fa-caret-right\}\} A replacement field consists of a Python expression for evaluation with optional debugging mode (=), type conversion (!), and format specification (:). \newline \{\{fa-caret-right\}\} Substituting into the f-string template: \newline \newline {\bf{`f" text \{f-expression = !conversion:format\_specifier\} text ..."`}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Format Specifier}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{`:fill align sign \# 0 width sep .precision type`}} \newline % Row Count 2 (+ 2) {\bf{Brief Summary of the Format Specification Mini-Language}} \newline % Row Count 4 (+ 2) `fill` : the padding character \newline % Row Count 5 (+ 1) `align` : alignment of text within the space \newline % Row Count 6 (+ 1) `sign` : how + and - are used preceding numbers \newline % Row Count 8 (+ 2) `\#` : alternate presentation format for some number types \newline % Row Count 10 (+ 2) `0` : sign-aware, zero-padding on numbers \newline % Row Count 11 (+ 1) `width` : the minimum total field width \newline % Row Count 12 (+ 1) `sep` : the separator character for numbers (',' or '\_') \newline % Row Count 14 (+ 2) `.precision` : how many digits displayed for floats; \{\{nl\}\}~~~~~~~~~~~~maximum field width for strings \newline % Row Count 18 (+ 4) `type` : the type of presentation to use based on data type% Row Count 20 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note}}: `sign`, `\#`, `0`, `sep`, `precision`, and `type` are of particular interest for {\bf{number formatting}}, the focus of this cheatsheet. For a general treatment of F-Strings, see my cheatsheet {\bf{Python F-Strings Basics}}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Column Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Format Specifier: Options}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{fill}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{char} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 2 (+ 0) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 2 (+ 0) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 2 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.88 cm} x{7.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sign and Separator Options}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Sign}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} + & a sign is used for both positive and negative numbers \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} - & a sign is used only for negative numbers (default) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} space & leading space for positive numbers and minus sign for negative \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Separator}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} , & comma. decimal integers and floats: thousands separator \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \_ & underscore. decimal integers and floats: thousands separator; b, o, x, and X types: separates every four digits \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.88 cm} x{3.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Sign and Separator}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# sign (+): a sign is attached to both positive and negative numbers} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `f"\{3:+\} or \{-3:+\}"` & '+3 or -3' \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# sign (-): a sign is only used for negative numbers} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} `f"\{3:-\} or \{-3:-\}"` & '3 or -3' \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# no sign is the same as sign (-)} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `f"\{3\} or \{-3\}"` & '3 or -3' \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# sign (space): a leading space for positive numbers and a minus sign for negative} \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} `f"\{3: \} or \{-3: \}"` & ' 3 or -3' \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# sign (+), separator (,)} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} `f"\{10000:+,\}"` & '+10,000' \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (?), align (\textless{}), sign (+), width (11), sep (,)} \tn % Row Count 15 (+ 2) % Row 11 \SetRowColor{white} `f"\{10000:?\textless{}+11,\}"` & '+10,000????' \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Page Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.2 cm} x{2.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sign Aware Padding}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Using the {\bf{= alignment option}} forces the padding determined by the fill and width specifications to be placed after the sign (if any) but before the digits. This is useful for number representations like '+000042'. This alignment option is only available for numeric types.} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{The {\bf{0-option}} produces sign aware zero-padding just like the = alignment option with a fill of '0'. It is produced by preceding the field width specification with '0'.} \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (?), align (=), sign (+), width (5)} \tn % Row Count 11 (+ 1) % Row 3 \SetRowColor{white} `f"\{3:?=+5\}"` & '+???3' \tn % Row Count 12 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (0), align (=), sign (+), width (5)} \tn % Row Count 13 (+ 1) % Row 5 \SetRowColor{white} `f"\{3:0=+5\}"` & '+0003' \tn % Row Count 14 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# sign (+), 0-option, width (5)} \tn % Row Count 15 (+ 1) % Row 7 \SetRowColor{white} `f"\{3:+05\}"` & '+0003' \tn % Row Count 16 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# 0-option, width (5)} \tn % Row Count 17 (+ 1) % Row 9 \SetRowColor{white} `f"\{3:05\}"` & '00003' \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.224 cm} x{4.536 cm} p{0.72 cm} p{0.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Integer Presentation Types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{\{\{ac\}\} {\bf{Decimal Integer Types}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `d` & Decimal Integer: the number in base 10 & & \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} None & Same as `d` & & \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `n` & Number. Same as `d`, except uses current locale setting for the appropriate number separator character & & \tn % Row Count 9 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{\{\{ac\}\} {\bf{Others}}} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} `b` & Binary: the number in base 2. & & \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} `o` & Octal: the number in base 8. & & \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} `x`, `X` & Hex: the number in base 16. `x` vs `X` signals lower vs upper case for digits \textgreater{} 9 & & \tn % Row Count 18 (+ 4) % Row 8 \SetRowColor{LightBackground} `c` & Unicode Character. Converts decimal integer to the corresponding unicode character & & \tn % Row Count 22 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{{\bf{`\#`-option}}: using the `\#`-option adds prefixes \newline ~~~~~`\#b`: adds prefix `0b`~~~~~~~~~~`\#o`: adds prefix `0o` \newline ~~~~~`\#x`: adds prefix `0x`~~~~~~~~~~`\#X`: adds prefix `0X`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Column Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.16 cm} x{5.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Integer Types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`i = 13`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# representing decimal 13 in binary, octal, and hexadecimal} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`f"binary: \{i:b\}; octal: \{i:o\}; hex: \{i:x\} or \{i:X\}"`} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} & 'binary: 1101; octal: 15; hex: d or D' \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# the \#-option adds prefixes} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`f"binary: \{i:\#b\}; octal: \{i:\#o\}; hex: \{i:\#x\} or \{i:\#X\}"`} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} & 'binary: 0b1101; octal: 0o15; hex: 0xd or 0XD' \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# binary with zero padding} \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} `f"\{i:08b\}"` & '00001101' \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# binary with prefix and zero padding} \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} `f"\{i:\#08b\}"` & '0b001101' \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# fill (0), align (=), \#-option, width (8), type (b)} \tn % Row Count 20 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`f"\{i:0=\#8b\}"`} \tn % Row Count 21 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{ac\}\}'0b001101'} \tn % Row Count 22 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# returns the unicode character with decimal representation 36} \tn % Row Count 24 (+ 2) % Row 15 \SetRowColor{white} `f"\{36:c\}" ` & '\$' \tn % Row Count 26 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# printing symbols with unicode decimal representations 33-41} \tn % Row Count 28 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`print(''.join({[}f"\{x:c\}" for x in range(33,42){]}))`} \tn % Row Count 29 (+ 1) % Row 18 \SetRowColor{LightBackground} & !"\#\$\%\&'() \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.16 cm} x{5.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Integer Types (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# separator (,) and type (d)} \tn % Row Count 1 (+ 1) % Row 20 \SetRowColor{white} `f"\{10000:,d\}"` & '10,000' \tn % Row Count 3 (+ 2) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# type d is the default for integers, so unnecessary} \tn % Row Count 5 (+ 2) % Row 22 \SetRowColor{white} `f"\{10000:,\}"` & '10,000' \tn % Row Count 7 (+ 2) % Row 23 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# separator (\_), type (b)} \tn % Row Count 8 (+ 1) % Row 24 \SetRowColor{white} `f"\{1200:\_b\}"` & '100\_1011\_0000' \tn % Row Count 10 (+ 2) % Row 25 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# to use locale specific number formatting, set the locale} \tn % Row Count 12 (+ 2) % Row 26 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`import locale`\{\{nl\}\}`locale.setlocale(locale.LC\_ALL, '')`} \tn % Row Count 14 (+ 2) % Row 27 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# then use format type 'n'} \tn % Row Count 15 (+ 1) % Row 28 \SetRowColor{white} `f"\{10**6:n\}"` & '1,000,000' \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Page Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{0.912 cm} x{2.204 cm} x{4.484 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Float Presentation Types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Type}} & {\bf{Name}} & {\bf{For a given precision `p`...}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `e`, `E` & Scientific Notation & The coefficient has 1 digit before and `p` digits after the decimal point. \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} `f`, `F` & \seqsplit{Fixed-Point} Notation & There are `p` digits following the decimal point. \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} `g`, `G` & General Format & Rounds number to `p` significant digits, then formats in either fixed-point or scientific notation depending on magnitude. \tn % Row Count 15 (+ 6) % Row 4 \SetRowColor{LightBackground} `\%` & Percentage & Multiplies the number by 100 and displays in fixed f format, followed by a percent sign. \tn % Row Count 19 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{\{\{fa-caret-right\}\} Default for float is p = 6. Default for decimal.Decimal is p large enough to show all coefficient digits. \newline \{\{fa-caret-right\}\} Lower case versus upper case: Determines whether scientific notation shows 'e' or 'E' and whether to use 'nan' versus 'NAN' and 'inf' versus 'INF'.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.36 cm} x{2.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Simple Float Comparisons}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# \%-style with precision=1} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `f"\{1/2:.1\%\}"` & '50.0\%' \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# Lower case e versus upper case E} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `f"\{1000:.1e\} vs \{1000:.1E\}"` & '1.0e+03 vs 1.0E+03' \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# Lower case f versus upper case F} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `f"\{1000:.1f\} vs \{1000:.1F\}"` & '1000.0 vs 1000.0' \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# Lower case g versus upper case G} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `f"\{1000:.1g\} vs \{1000:.1G\}"` & '1e+03 vs 1E+03' \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# Lower versus upper case NaN representation} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} `nan = float('nan')` \{\{nl\}\} `f"\{nan:f\} vs \{nan:F\}"` & \{\{nl\}\}'nan vs NAN' \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{General Format (g or G) Rule}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Given the number `y` and the precision specification `p`:} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{(a)}} Let `exp` = the exponent of `y` in scientific notation.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{(b)}} Is `-4 \textless{}= exp \textless{} p`?} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{"Yes": Format `y` using fixed-point notation and a new precision: `p' = p - (1 + exp)` where `p` represents the number of significant digits before any trailing zeros are removed. (In fixed-point format, the precision, here `p'`, determines the number of digits following the decimal point. When it is positive, the value `1 + exp` can be interpreted as the total number of digits before the decimal; otherwise, it represents the total number of zeros after the decimal point but before the significant digits.)} \tn % Row Count 16 (+ 11) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{"No": Format `y` using scientific notation and a new precision: `p' = p - 1` where `p` represents the total number of significant digits in the coefficient before any trailing zeros are removed. (In scientific format, the precision determines the number of digits displayed after the decimal point of the coefficient. So, the total number of digits displayed will be: precision + 1 = `p' + 1 = p`.)} \tn % Row Count 24 (+ 8) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{(c)}} With the \#-option (`\#g`, `\#G`): Keep any trailing zeros and the decimal point. \{\{nl\}\}Without the \#-option (`g`, `G`): If the result has insignificant trailing zeros, remove them as well as an unflanked decimal point.} \tn % Row Count 29 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Note: In both cases in (b), the original precision specification `p` determines the effective number of significant digits up until insignificant trailing zeros are removed in (c).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.448 cm} x{2.232 cm} p{0.792 cm} x{1.728 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{General Format (g or G): Illustration}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{\#.4g}} & {\bf{.4g}} & {\bf{exp}} & {\bf{format type}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} (keep trailing zeros) & (no trailing zeros) & ~ & \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} 1.000e-06 & 1e-06 & -6 & e \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} 1.000e-05 & 1e-05 & -5 & e \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} 0.0001000 & 0.0001 & -4 & f \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} 0.001000 & 0.001 & -3 & f \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} 0.01000 & 0.01 & -2 & f \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} 0.1000 & 0.1 & -1 & f \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} 1.000 & 1 & 0 & f \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} 10.00 & 10 & 1 & f \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} 100.0 & 100 & 2 & f \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} 1000. & 1000 & 3 & f \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} 1.000e+04 & 1e+04 & 4 & e \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{In this illustration, the precision specification is `p = 4`; so, for `exp` from -4 to 3, fixed-point notation (`f`) is used; otherwise, scientific notation (`e`) is used.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Page Break}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.76 cm} x{3.42 cm} x{3.42 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Types with Conditional Formatting}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Type}} & {\bf{Interpretation}} & \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `g`, `G` & = `e` or `f` (`E` or `F`) according to the General Format Rule & \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} none & = `s` for strings & \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} & = `d` for integers & \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} & = `g` for floats & When fixed-point notation is used, it always includes at least one digit past the decimal point. The precision varies to represent the given value faithfully. \tn % Row Count 17 (+ 9) % Row 5 \SetRowColor{white} `n` & = `d` for integers & \tn % Row Count 19 (+ 2) % Row 6 \SetRowColor{LightBackground} & = `g` for floats & Uses the current locale setting for separator formatting. \tn % Row Count 23 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Examples: Both Integers and Floats}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`si = 55` ~~\# small integer \{\{nl\}\}`li = 666666` ~~\# large integer \{\{nl\}\}`sf = 7.77777` ~~\# small float \{\{nl\}\}`lf = 8888.88` ~~\# large float \{\{nl\}\}`sc = 9e6` ~~\# scientific notation} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# none -{}- no type specified\{\{nl\}\} `f"\{si\} \{li\} \{sf\} \{lf\} \{sc\}"`} \tn % Row Count 7 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55 ~~666666 ~~7.77777 ~~8888.88 ~~9000000.0'} \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# n -{}- number type \{\{nl\}\} `f"\{si:n\} \{li:n\} \{sf:n\} \{lf:n\} \{sc:n\}"`} \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55 ~~666666 ~~7.77777 ~~8888.88 ~~9e+06'} \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# n with \#-option \{\{nl\}\} `f"\{si:\#n\} \{li:\#n\} \{sf:\#n\} \{lf:\#n\} \{sc:\#n\}"`} \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55 ~~666666 ~~7.77777 ~~8888.88 ~~9.00000e+06'} \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# n with precision = 3 (precision not allowed for integers) \{\{nl\}\}`f"\{si:.3n\} \{li:.3n\} \{sf:.3n\} \{lf:.3n\} \{sc:.3n\}"`} \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}\{\{fa-frown-o\}\}} \tn % Row Count 21 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# e with precision = 3 (forces 3 digits after decimal point) \{\{nl\}\} `f"\{si:.3e\} \{li:.3e\} \{sf:.3e\} \{lf:.3e\} \{sc:.3e\}"`} \tn % Row Count 24 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# including the \#-option gives the same result here \{\{nl\}\} `f"\{si:\#.3e\} \{li:\#.3e\} \{sf:\#.3e\} \{lf:\#.3e\} \{\{nl\}\}\{sc:\#.3e\}"`} \tn % Row Count 27 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'5.500e+01 ~~6.667e+05 ~~7.778e+00 ~~8.889e+03 ~~9.000e+06'} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Examples: Both Integers and Floats (cont)}} \tn % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# f with precision = 3 (forces 3 digits after decimal point) \{\{nl\}\} `f"\{si:.3f\} \{li:.3f\} \{sf:.3f\} \{lf:.3f\} \{sc:.3f\}"`} \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# including the \#-option gives the same result here \{\{nl\}\} `f"\{si:\#.3f\} \{li:\#.3f\} \{sf:\#.3f\} \{lf:\#.3f\} \{\{nl\}\}\{sc:\#.3f\}"`} \tn % Row Count 6 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55.000 ~~666666.000 ~~7.778 ~~8888.880 ~~9000000.000'} \tn % Row Count 9 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# g with precision = 3 (shows no unnecesary zeros or decimal points) \{\{nl\}\} `f"\{si:.3g\} \{li:.3g\} \{sf:.3g\} \{lf:.3g\} \{sc:.3g\}"`} \tn % Row Count 12 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55 ~~6.67e+05 ~~7.78 ~~8.89e+03 ~~9e+06'} \tn % Row Count 14 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# g with \#-option (forces showing 3 significant digits) \{\{nl\}\} `f"\{si:\#.3g\} \{li:\#.3g\} \{sf:\#.3g\} \{lf:\#.3g\} \{\{nl\}\}\{sc:\#.3g\}"`} \tn % Row Count 17 (+ 3) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'55.0 ~~6.67e+05 ~~7.78 ~~8.89e+03 ~~9.00e+06'} \tn % Row Count 19 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# In float types generally, the \#-option forces a decimal point occurrence even when no digits follow it \{\{nl\}\}`f"\{10:\#.0e\} \{10:\#.0f\} \{10:\#.0g\}"`} \tn % Row Count 22 (+ 3) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}'1.e+01 ~~10. ~~1.e+01'} \tn % Row Count 24 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{References}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"A Guide to the Newer Python String Format Techniques" by John Sturtz at {\emph{Real Python}}: \seqsplit{https://realpython.com/python-formatted-output/\#the-python-formatted-string-literal-f-string}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"Python 3's f-Strings: An Improved String Formatting Syntax (Guide)" by Joanna Jablonski at {\emph{Real Python}}: \seqsplit{https://realpython.com/python-f-strings/}} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"Format String Syntax" including "Format Specification Mini-Language" from the page "`string` -{}- Common string operations": \seqsplit{https://docs.python.org/3/library/string.html\#format-string-syntax}} \tn % Row Count 13 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"2.4.3. Formatted string literals" from the page "2. Lexical analysis": \seqsplit{https://docs.python.org/3/reference/lexical\_analysis.html\#formatted-string-literals}} \tn % Row Count 17 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"PEP 498 -{}- Literal String Interpolation": \seqsplit{https://www.python.org/dev/peps/pep-0498/}} \tn % Row Count 20 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-caret-right\}\}"Python String Format Cookbook": \seqsplit{https://mkaz.blog/code/python-string-format-cookbook/}} \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}