\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{mutanclan (mutanclan)} \pdfinfo{ /Title (python-string-formatting.pdf) /Creator (Cheatography) /Author (mutanclan (mutanclan)) /Subject (python string 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}{198C15} \definecolor{LightBackground}{HTML}{F7FBF7} \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 string formatting Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{mutanclan (mutanclan)} via \textcolor{DarkBackground}{\uline{cheatography.com/79625/cs/19406/}}} \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}mutanclan (mutanclan) \\ \uline{cheatography.com/mutanclan} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 20th April, 2019.\\ Updated 20th April, 2019.\\ 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{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Field definitions}} \tn % Row 0 \SetRowColor{LightBackground} \{\{noshy\}\}replacement\_field & \{\{nobreak\}\}`"\{" {[}field\_name{]} {[}"!" conversion{]} {[}":" format\_spec{]} "\}"` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} field\_name & \{\{nobreak\}\}`arg\_name ("." attribute\_name | "{[}" element\_index "{]}")*` \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} arg\_name & `{[}identifier | digit+{]}` \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \{\{noshy\}\}attribute\_name & `identifier` \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \{\{noshy\}\}element\_index & `digit+ | index\_string` \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} index\_string & \{\{noshy\}\}`\textless{}any source character except "{]}"\textgreater{} +` \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} conversion & `"r" | "s" | "a"` \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} format\_spec & \{\{noshy\}\}`Format Specification Mini-Language` \tn % Row Count 16 (+ 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}{field\_name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The {\emph{replacement\_field}} can start with a {\emph{field\_name}} to specify the object whose value is to be formatted and inserted. \newline % Row Count 3 (+ 3) The {\emph{field\_name}} begins with an {\emph{arg\_name}}. The {\emph{arg\_name}} can be followed by any number of index or attribute expressions.% Row Count 6 (+ 3) } \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}{arg\_name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{An {\emph{arg\_name}} is either a number or a keyword. If it's a number it refers to a positional argument. If it's a keyword, it refers to a named keyword argument. If the numerical {\emph{arg\_names}} in a format string are 0,1,2 in sequence, the can be omitted (They are automatically inserted).% Row Count 6 (+ 6) } \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}{attribute\_name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{noshy\}\}An expression of the form `'.name'` selects the named attribute using {\bf{getattr()}}% Row Count 2 (+ 2) } \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}{element\_index}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{noshy\}\}An expression of the form `'{[}index{]}'` does an index lookup using {\bf{\_\_getitem\_\_()}}. \newline % Row Count 2 (+ 2) For example: \newline % Row Count 3 (+ 1) List index: `{[}0{]}` \newline % Row Count 4 (+ 1) Dictionary: `{[}name{]}`% Row Count 5 (+ 1) } \tn \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}{conversion}} \tn % Row 0 \SetRowColor{LightBackground} !s & calls {\bf{str()}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} !r & calls {\bf{repr()}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} !a & calls {\bf{ascii()}} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{The {\emph{conversion}} field forces a type conversion {\bf{before}} formatting, so not by the {\bf{\_\_format\_\_()}} method of the value itself.} \tn \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 types}} \tn % Row 0 \SetRowColor{LightBackground} `s` & String format. This is the default for strings \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} None & The same as `s` \tn % Row Count 3 (+ 1) \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}{Integer presentation types}} \tn % Row 0 \SetRowColor{LightBackground} `b` & Binary format. Outputs the number in base 2 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `c` & Character. Converts the integer to unicode \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `d` & Decimal integer. Outputs number in base 10 \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `o` & Octal format. Outputs number in base 8 \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `x` & Hex format. Outputs number in base 16 using lowercase letters \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `X` & Hex format. Outputs number in base 16 using uppercase letters \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} `n` & Number. Same as `d` but uses current locale setting for the separator \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} None & Same as `d` \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Format Specification Mini-Language}} \tn % Row 0 \SetRowColor{LightBackground} \{\{noshy\}\}format\_spec & \{\{noshy\}\}`{[}{[}fill{]}align{]}{[}sign{]}{[}\#{]}{[}0{]}{[}width{]}{[}grouping\_option{]}{[}.precision{]}{[}type{]}` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} fill & `\textless{}any character\textgreater{}` \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} align & `"\textless{}" | "\textgreater{}" | "=" | "\textasciicircum{}"` \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} sign & `"+" | "-" | " "` \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} width & `digit+` \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \{\{noshy\}\}grouping\_option & `"\_" | ","` \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} precision & `digit+` \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} type & \{\{noshy\}\}`"b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "\%"` \tn % Row Count 16 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.2 cm} x{6.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{fill, sign and align}} \tn % Row 0 \SetRowColor{LightBackground} `\textless{}` & Force left-alignment within available space \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `\textgreater{}` & Force rigth-alignment within available space \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `=` & Only valid for numeric types. Forces the padding to be placed after the {\emph{sign}} but before the digits \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} `\textasciicircum{}` & Forces the field to be centered within available space \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} `+` & Use a {\emph{sign}} for both positiv and negative numbers \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} `-` & Use {\emph{sign}} only for negative numbers \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{`space`} & Use a leading space for positiv numbers and a minus sign for negative numbers \tn % Row Count 16 (+ 3) % Row 7 \SetRowColor{white} `\#` & Causes the alternate form to be used for the conversion. binary: `0b`, octal: `0o` and hex: `0x`. For floats, complex and Decimal types that causes to contain a trailing decimal-point even if no digits follow it \tn % Row Count 23 (+ 7) % Row 8 \SetRowColor{LightBackground} `,` & Use `,` for thousands separator \tn % Row Count 24 (+ 1) % Row 9 \SetRowColor{white} `\_` & Use `\_` for thousands separator \tn % Row Count 25 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{If an {\emph{align}} value is specified it can be preceded by a {\emph{fill}} character, that can be any character (default is space) \newline \newline The {\emph{sign}} option is only valid on numeric types} \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}{width and precision}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{width}} is a decimal integer defining the minimum field width. A leading `0` enables sign-aware zero-padding for numeric types. \newline % Row Count 3 (+ 3) {\emph{precision}} is a decimal number indicating how many digits should be displayed after the decimal point. For non-number types it indicates the maximum field-size. Not allowed for integer values% Row Count 7 (+ 4) } \tn \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}{Floating point and decimal presentation types}} \tn % Row 0 \SetRowColor{LightBackground} `e` & Exponent notation using the letter {\bf{e}} to indicate the exponent. Default {\emph{precision}} is `6` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `E` & Exponent notation. Same as `e` but with uppercase {\bf{E}} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} `f` & Fixed-point notation. Default {\emph{precision}} is `6` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} `F` & Fixed-point notation. Same as `f` but converts `nan` to `NAN` and `inf` to `INF` \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} `g` & General format. If {\emph{precision}} is `p\textgreater{}=1` this rounds the number to `p` significant digits. Output format is either fixed-point or in scientific notation, depending on the magnitude \tn % Row Count 15 (+ 5) % Row 5 \SetRowColor{white} `G` & General format. Same as `g` but switches to `E` if the number gets too large. \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} `n` & Number. Same as `g` but use current locale for the number separator character \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} `\%` & Percentage. Multiplies number by 100 and displays it in `f` format followed by a percentage sign \tn % Row Count 24 (+ 3) % Row 8 \SetRowColor{LightBackground} None & Same as `g` but fixed-point notation has at least one digit past the decimal point \tn % Row Count 27 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}