\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-basics.pdf) /Creator (Cheatography) /Author (Brian Allan (BrianAllan)) /Subject (Python F-Strings Basics 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}{06962D} \definecolor{LightBackground}{HTML}{F7FBF8} \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 Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Brian Allan (BrianAllan)} via \textcolor{DarkBackground}{\uline{cheatography.com/133708/cs/29498/}}} \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 24th February, 2023.\\ 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}{What are F-Strings?}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{What are F-Strings?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Strings prefixed with 'f' or 'F' and containing Python expres​sions inside curly braces for evaluation at run time. They are more formally known as "formatted string literals" and were introduced with Python 3.6.} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{How are F-Strings Useful?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}They provide a concise, readable way to include the value of a Python expres​sion with formatting control inside strings.} \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{What Are Other Ways to Format Strings in Python?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\{\{fa-caret-right\}\} `\{\{lang-py\}\}str.format()`: the string `\{\{lang-py\}\}format()` method \{\{nl\}\} \{\{fa-caret-right\}\} \%-formatting: old string formatting using the string modulo/percent operator \% \{\{nl\}\} \{\{fa-caret-right\}\}`\{\{lang-py\}\}string.Template`: template class of the string module} \tn % Row Count 18 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \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" {\emph{text}} \{replacement\_field\} {\emph{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 in this representative pattern but single or triple quotes could also be used. \newline \{\{fa-caret-right\}\} F-strings may consist of just a replacement field: \newline ~ ~ ~ ~ {\bf{ `\{\{lang-py\}\}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 an expression with optional debugging mode (`=`), type conversion (`!`), and format specification (`:`). \newline \{\{fa-caret-right\}\} Substituting into the f-string template: \newline \newline {\bf{f" {\emph{text}} \{f-expression = !conversion:format\_specifier\} {\emph{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}{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}{p{0.72 cm} x{5.04 cm} p{0.72 cm} p{0.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Conversion Field Options}} \tn % Row 0 \SetRowColor{LightBackground} `!s` & calls `str()` on the value of the f-expression & & \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `!r` & calls `repr()` on the value of the f-expression & & \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `!a` & calls `ascii()` on the value of the f-expression & & \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{\{\{fa-caret-right\}\} `str()` returns string value representations that are human readable for the end user. \newline \{\{fa-caret-right\}\} `repr()` returns string value representations for the interpreter and developers. \newline \{\{fa-caret-right\}\} `!s`, `!r`, `!a` conversions are redundant since arbitrary expressions are allowed in the replacement fields; so, one could just as easily replace `!r` by using `repr()` on the f-expression and similarly for the others.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Conversion Field}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`x, name = 'cat', 'Sophia'`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# explicitly using the string conversion `!s` (the default)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `f"The black \{x!s\}"` & 'The black cat' \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# using the string conversion `!r` adds quotes} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} `f"The black \{x!r\}"` & "The black 'cat'" \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `f"Her name is \{name!r\}."` & "Her name is 'Sophia'." \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# `!r` is equivalent to using `repr()` on the expression} \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} `f"The black \{repr(x)\}"` & "The black 'cat'" \tn % Row Count 12 (+ 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}{Format Specifier}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{~ \newline % Row Count 1 (+ 1) {\bf{`:fill align sign \# 0 width sep .precision type` }} \newline % Row Count 3 (+ 2) ~ \newline % Row Count 4 (+ 1) {\bf{Brief Summary of the Format Specification Mini-Language}} \newline % Row Count 6 (+ 2) `fill` : the padding character \newline % Row Count 7 (+ 1) `align` : alignment of text within the space \newline % Row Count 8 (+ 1) `sign` : how + and - are used preceding numbers \newline % Row Count 10 (+ 2) `\#` : alternate presentation format for some number types \newline % Row Count 12 (+ 2) `0` : sign-aware, zero-padding on numbers \newline % Row Count 13 (+ 1) `width` : the minimum total field width \newline % Row Count 14 (+ 1) `sep` : the separator character for numbers (',' or '\_') \newline % Row Count 16 (+ 2) `.precision` : determines how many digits displayed for floats;\{\{nl\}\}~~~~~~~~~~~~maximum field width for strings \newline % Row Count 20 (+ 4) `type` : the type of presentation to use based on data type% Row Count 22 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Note: `sign`, `\#`, `0`, `sep`, `precision`, and `type` are of particular interest for {\bf{number formatting}}. For information about number formatting, see my cheatsheet {\bf{Python F-Strings Number Formatting}}.} \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}{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.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Presentation Type}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{simplified form: {\bf{`f" {\emph{text}} \{f-expression:type\} {\emph{text}} ..."`}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `s` & String format. This is the default type for strings and may be omitted. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} None & Same as `s` \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-caret-right\}\}Where the value of the f-expression is a string, the replacement field could make explicit the string presentation type {\bf{`\{f-expression:s\}`}} but {\bf{`:s`}} can be omitted since this is the default for strings. \newline \{\{fa-caret-right\}\}`.precision` can be used with strings to enforce a maximum field width: `\{f-expression:.precision\}`} \tn \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: Simple F-Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`x = 'cat'`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# f-expression can be a string} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `f"The black \{'cat'\}"` & 'The black cat' \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# f-expression can be a variable with a string value} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} `f"The black \{x\}"` & 'The black cat' \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# error when neither a string nor a defined variable} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} `f"The black \{cat\}"` & \{\{fa-frown-o\}\} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# including f-string in string concatenation} \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `'The ' 'black ' f"\{x\}"` & 'The black cat' \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# including f-string in string concatenation} \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} `'The ' + 'black ' + f"\{x\}"` & 'The black cat' \tn % Row Count 14 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# f-strings can use single, double, or triple quotes} \tn % Row Count 16 (+ 2) % Row 12 \SetRowColor{LightBackground} `f'The ' f"black " f'''cat'''` & 'The black cat' \tn % Row Count 18 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# text inside the f-string must contain a different kind of quotes than the outer quotes} \tn % Row Count 20 (+ 2) % Row 14 \SetRowColor{LightBackground} `f'The 'black' cat'` & \{\{fa-frown-o\}\} \tn % Row Count 21 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# f-expressions need different quotes than the outer quotes} \tn % Row Count 23 (+ 2) % Row 16 \SetRowColor{LightBackground} `f'The black \{'cat'\}'` & \{\{fa-frown-o\}\} \tn % Row Count 24 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# Or, f-expressions can use a variable to represent the string} \tn % Row Count 26 (+ 2) % Row 18 \SetRowColor{LightBackground} `f'The black \{x\}'` & 'The black cat' \tn % Row Count 27 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# for debugging, an equal sign can be used after an f-expression to display the expression text and its value} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4.88 cm} x{3.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Simple F-Strings (cont)}} \tn % Row 20 \SetRowColor{LightBackground} `f"The black \{x=\}"` & "The black x='cat'" \tn % Row Count 2 (+ 2) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# backslash escapes can be used in text} \tn % Row Count 3 (+ 1) % Row 22 \SetRowColor{LightBackground} `f"The black \textbackslash{}'cat\textbackslash{}'"` & "The black 'cat'" \tn % Row Count 5 (+ 2) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# doubled curly braces for a single curly brace in text} \tn % Row Count 7 (+ 2) % Row 24 \SetRowColor{LightBackground} `f"The \{\{black\}\} \{x\}"` & 'The \{black\} cat' \tn % Row Count 9 (+ 2) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# using .precision to enforce a maximum field width of 7} \tn % Row Count 11 (+ 2) % Row 26 \SetRowColor{LightBackground} `f"\{'The black cat':.7\}"` & 'The bla' \tn % Row Count 13 (+ 2) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# Multi-line f-strings} \tn % Row Count 14 (+ 1) % Row 28 \SetRowColor{LightBackground} `f"""`\{\{nl\}\}`The black` \{\{nl\}\}`cat"""` & \{\{nl\}\}'\textbackslash{}nThe black\textbackslash{}ncat' \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.28 cm} x{2.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Complex F-Expressions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`colors = {[}'blue', 'green', 'yellow', 'red'{]}`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`pets = \{'cats': 2, 'dogs': 1\}`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# f-expression with indexing and a method call} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`f"\{colors{[}2{]}.title()\} is my favorite color."`} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'Yellow is my favorite color.'} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# f-expression with slicing} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`f"\{colors{[}:3{]}\}"`} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}"{[}'blue', 'green', 'yellow'{]}"} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# f-expression with a function call} \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`f"There are \{len(colors)\} options."`} \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'There are 4 options.'} \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# using dictionary keys and an arithmetical operation} \tn % Row Count 13 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`f"She has \{pets{[}'cats'{]} + pets{[}'dogs'{]}\} pets."`} \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'She has 3 pets.'} \tn % Row Count 15 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# for debugging, an equal sign can be used to display the f-expression and its value} \tn % Row Count 17 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`f"She has \{pets{[}'cats'{]} + pets{[}'dogs'{]} = \} pets."`} \tn % Row Count 19 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}"She has pets{[}'cats'{]} + pets{[}'dogs'{]} = 3 pets."} \tn % Row Count 21 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# using a conditional expression} \tn % Row Count 22 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`f"She \{'has' if (pets{[}'cats'{]} \textgreater{} pets{[}'dogs'{]}) else 'does not have'\} more cats than dogs."`} \tn % Row Count 24 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'She has more cats than dogs.'} \tn % Row Count 25 (+ 1) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# dictionary keys used by f-string must have different kind of quotes} \tn % Row Count 27 (+ 2) % Row 21 \SetRowColor{white} `f'She has \{pet{[}'cats'{]}\}.'` & \{\{fa-frown-o\}\} \tn % Row Count 29 (+ 2) \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}{Formatting: Fill, Align, Width}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{`f"\{f-expression:fill align width\}"`}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} width & a decimal integer defining the minimum total field width. If not specified, the field width is determined by the content. \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} align & determines the alignment of text within the available space -{}- left-aligned (\textless{}), right-aligned (\textgreater{}), or centered (\textasciicircum{}) \tn % Row Count 9 (+ 4) % Row 3 \SetRowColor{white} fill & determines the character to use for padding to achieve the minimum total field width. The default is the space character. \tn % Row Count 13 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Alignment Options}} \tn % Row 0 \SetRowColor{LightBackground} \textless{} & Left-alignment (the default for most objects) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \textgreater{} & Right-alignment (the default for numbers) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \textasciicircum{} & Centered \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} = & Sign-aware padding. For numeric types: Places the padding before the digits but after the sign (if any) \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Fill, Align, Width}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (.), right align (\textgreater{}), width (12)} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `f"Go \{'right':.\textgreater{}12\}"` & 'Go .......right' \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (!), left align (\textless{}), width (12)} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `f"Go \{'left':!\textless{}12\}"` & 'Go left!!!!!!!!' \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# fill (*), center align (\textasciicircum{}), width (12)} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `f"Go \{'center':*\textasciicircum{}12\}"` & 'Go ***center***' \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# nested replacement fields allow for the use of variables in the format specifier} \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`fill, align, width = '*', '\textasciicircum{}', 12` \{\{nl\}\} `f"Go \{'center':\{fill\}\{align\}\{width\}\}"`} \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'Go ***center***'} \tn % Row Count 14 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# NOTE: the fill is the symbol (*), not the symbol as string ('*')} \tn % Row Count 16 (+ 2) % Row 10 \SetRowColor{LightBackground} `f"Go \{'center':'*'\textasciicircum{}12\}"` & \{\{fa-frown-o\}\} \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# BUT: when using a nested replacement field for fill, the value of the variable has to be the string of the symbol ('*'), not the symbol (*)} \tn % Row Count 21 (+ 3) % Row 12 \SetRowColor{LightBackground} `fill = '*'` \{\{nl\}\}`f"Go \{'center':\{fill\}\textasciicircum{}12\}"` & \{\{nl\}\}'Go ***center***' \tn % Row Count 24 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# Default fill when not specified is the space character} \tn % Row Count 26 (+ 2) % Row 14 \SetRowColor{LightBackground} `f"Go \{'right':\textgreater{}10\}"` & 'Go~~~~~~right' \tn % Row Count 29 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# Default for strings when not specified: fill (space), left align (\textless{})} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Examples: Fill, Align, Width (cont)}} \tn % Row 16 \SetRowColor{LightBackground} `f"Go \{'left':10\}"` & 'Go left~~~~~~' \tn % Row Count 3 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# Default for numbers when not specified: fill (space), right align (\textgreater{})} \tn % Row Count 5 (+ 2) % Row 18 \SetRowColor{LightBackground} `f"Total: \{5:8\}"` & 'Total: ~~~~~~~5' \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Example: For Loop and Nested Replacement Fields}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{width = 12 \newline for text, fill in zip({[}'left', 'center', 'right'{]}, '\textless{}\textasciicircum{}\textgreater{}'): \newline align = fill \newline print(f"\{text:\{fill\}\{align\}\{width\}\}")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}left\textless{}\textless{}\textless{}\textless{}\textless{}\textless{}\textless{}\textless{} \newline \textasciicircum{}\textasciicircum{}\textasciicircum{}center\textasciicircum{}\textasciicircum{}\textasciicircum{} \newline \textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}right} \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}{Example: Text Template Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# function with f-string as message template \newline def message(name, num): \newline return f"\{name.title()\}'s number is \{num\}." \newline \newline message('jenny', 8675309)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}"Jenny's number is 8675309."} \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}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Example: Row Template for Table Creation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# data for table \newline presidents = {[} \newline {[}'George Washington', 1, 1789, 1797{]}, \newline {[}'John Adams', 2, 1797, 1801{]}, \newline {[}'Thomas Jefferson', 3, 1801, 1809{]} \newline {]} \newline \newline \# create row template function \newline def row(name, num, start, end): \newline return f"| \{name:\textless{}20\} | \{num:2\} | \{start\} - \{end\} |" \newline \# print rows iteratively \newline for p in presidents: \newline print(row(p{[}0{]}, p{[}1{]}, p{[}2{]}, p{[}3{]}))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{| George Washington | ~1 | 1789 - 1797 | \newline | John Adams ~~~~~~~~~~~ | ~2 | 1797 - 1801 | \newline | Thomas Jefferson ~~~| ~3 | 1801 - 1809 |} \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}{Example: Title Bar}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{lang-py\}\}fill, align, width = '*', '\textasciicircum{}', 21 \newline for text in {[}'', ' Title ', ''{]}: \newline print(f"\{text:\{fill\}\{align\}\{width\}\}")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\}********************* \newline ******* Title ******* \newline *********************} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.96 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Datetime Formatting with F-Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Some Python objects have their own format specifiers to replace the standard ones. An example of this behavior is found in the `date`, `datetime`, and `time` objects of the `datetime` module.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# using the `datetime` module to obtain today's date} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} `\{\{lang-py\}\}import datetime`\{\{nl\}\} `\{\{lang-py\}\}today = datetime.date.today()`\{\{nl\}\} `\{\{lang-py\}\}f"\{today\}"` & \{\{nl\}\}\{\{nl\}\}'2022-03-14' \tn % Row Count 11 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\# object-specific formatting directives used in place of the standard format specifiers} \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`\{\{lang-py\}\}f"\{today:\%A, \%B \%d, \%Y\}"`} \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'Monday, March 14, 2022'} \tn % Row Count 15 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\# the output is the same as using the `strftime()` method of the `datetime` module} \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`\{\{lang-py\}\}today.strftime("\%A, \%B \%d, \%Y")`} \tn % Row Count 18 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{ar\}\}'Monday, March 14, 2022'} \tn % Row Count 19 (+ 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}{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{1.824 cm} x{2.812 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Short List of Datetime Formatting Directives}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Directive}} & {\bf{Meaning}} & {\bf{Example}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `\%A` & Weekday full name & Sunday, Monday,... \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `\%a` & Weekday abbreviated & Sun, Mon,... \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `\%B` & Month full name & January, February,... \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `\%b` & Month abbreviated & Jan, Feb,... \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `\%d` & Day of Month & 01, 02, 03,... \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} `\%Y` & Year with Century & 2019, 2020,... \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} `\%y` & Year without Century & 19, 20,... \tn % Row Count 15 (+ 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}