\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{eazykaye} \pdfinfo{ /Title (python3-f-strings.pdf) /Creator (Cheatography) /Author (eazykaye) /Subject (Python3 F-Strings 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}{155494} \definecolor{LightBackground}{HTML}{F0F4F8} \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{Python3 F-Strings Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{eazykaye} via \textcolor{DarkBackground}{\uline{cheatography.com/164268/cs/34422/}}} \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}eazykaye \\ \uline{cheatography.com/eazykaye} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 30th September, 2022.\\ Updated 28th October, 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*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Number Formatting}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = 210 \newline \newline \# decimal places \newline print(f"number: \{number:.2f\}") \newline \newline \# hex conversion \newline print(f"hex: \{number:\#0x\}") \newline \newline \# binary conversion \newline print(f"binary: \{number:b\}") \newline \newline \# octal conversion \newline print(f"octal: \{number:o\}") \newline \newline \# scientific notation \newline print(f"scientific: \{number:e\}") \newline \newline \# total number of characters \newline print(f"Number: \{number:09\}") \newline \newline \# use comma separator \newline apple\_marketcap = 2.626 * 10e12 \newline print(f"\{apple\_marketcap = :,\}") \# comma separator \newline \newline \#set 2 decimal places and add a percentage sign to the end \newline percentage = 10.39439423 \newline print(f"\{percentage = :.2\%\}") \# percentage \newline \newline \#set 4 decimal places \newline number= 10.39439423 \newline print(f"\{number:.4f\}") \newline \newline \#Output: \newline number: 420.00 \newline hex: 0x1a4 \newline binary: 110100100 \newline octal: 644 \newline scientific: 4.200000e+02 \newline Number: 000000420 \newline apple\_marketcap = 26,260,000,000,000.0 \newline percentage = 1039.44\% \newline 10.3944} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Repr \& Str}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# \_\_repr\_\_= developer friendly \newline \# \_\_str\_\_ = user friendly \newline \# write !r to tell Python to print out the repr method instead. \newline \newline from dataclasses import dataclass \newline \newline @dataclass \newline class Person: \newline name : str \newline age : int \newline \newline def \_\_str\_\_(self) -\textgreater{} str: \newline return f"\{self.name\} is \{self.age\} years old" \newline \newline Elon = Person("Elon Musk", 51) \newline print(f"\{Elon\}") \# str \newline print(f"\{Elon!r\}") \# repr \newline \newline \#output: \newline Elon Musk is 51 years old \newline Person(name='Elon Musk', age=51)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Date formatting}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import datetime \newline \newline today = \seqsplit{datetime.datetime.utcnow()} \newline print(f"datetime : \{today\}\textbackslash{}n") \newline \newline \# no microseconds \newline print(f"date time: \{today:\%m/\%d/\%Y \%H:\%M:\%S\}") \newline \newline \# date only \newline print(f"date: \{today:\%m/\%d/\%Y\}") \newline \newline \# time only \newline print(f"time: \{today:\%H:\%M:\%S.\%f\}") \newline \newline \# time with AM/PM \newline print(f"time: \{today:\%H:\%M:\%S \%p\}") \newline \newline \# 24-hour format \newline print(f"time: \{today:\%H:\%M\}") \newline \newline \# Locale's appropriate date and time representation \newline print(f"locale appropriate: \{today:\%c\}") \newline \newline \# weekday \newline print(f"weekday: \{today:\%A\}") \newline \newline \# day of the year \newline print(f"day of year: \{today:\%j\}") \newline \newline \# how far are we into the year? \newline day\_of\_year = f"\{today:\%j\}" \newline print(f"progress \% year: \{int(day\_of\_year)/365 * 100:.2f\}\%") \newline \newline \#output \newline datetime : 2022-09-13 05:44:17.546036 \newline \newline date time: 09/13/2022 05:44:17 \newline date: 09/13/2022 \newline time: 05:44:17.546036 \newline time: 05:44:17 AM \newline time: 05:44 \newline locale appropriate: Tue Sep 13 05:44:17 2022 \newline weekday: Tuesday \newline day of year: 256 \newline progress \% year: 70.14\%} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{strftime Format Codes: \seqsplit{https://docs.python.org/3/library/datetime.html\#strftime-and-strptime-format-codes}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Debugging}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# works in Python 3.8+ \newline \newline x = 10 \newline y = 20 \newline \newline print(f"x = \{x\}, y = \{y\}") \newline print(f"\{x = \}, \{y = \}") \newline \newline \# math operations \newline print(f"\{x {\emph{ y = \}") \newline \newline \#output: \newline x = 10, y = 20 \newline x = 10, y = 20 \newline x }} y = 200} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Multi-line f-string}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{company\_name = "Tesla" \newline employee\_count = 100000 \newline mission = "To accelerate the world's transition to sustainable energy" \newline \newline print(f""" \newline Company: \{company\_name\} \newline \# of employees: \{employee\_count:,\} \newline Mission: \{mission\} \newline """) \newline \newline \#output: \newline Company: Tesla \newline \# of employees: 100,000 \newline Mission: To accelerate the world's transition to sustainable energy} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Alignment}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = 4 \newline \newline \# n stands for the width of space to print the variable number starting from the string "is" (inclusive of the variable itself) \newline print(f"number is \{number:4\}") \newline \#number is 4 \newline \newline for number in range(1, 5): \newline print(f"the number is \{number:\{number\}\}") \newline \newline \# the number is 1 \newline \# the number is 2 \newline \# the number is 3 \newline \# the number is 4 \newline \newline left = "left text" \newline center = "center text!" \newline right = "right text" \newline \newline \# left:\textgreater{}20 means given a width of 20 characters print out the string "left text" starting from the left. \newline print(f"\{left:\textgreater{}20\}") \# left align \newline \# left text \newline \newline \# center:\textasciicircum{}20 that means to leave whatever space is left on the left and right. Since the string "center text!" is 12 characters, the left and right would have four characters of white space. \newline print(f"\{center:\textasciicircum{}20\}") \# center align \newline \# center text! \newline \newline print(f"\{right:\textless{}20\}") \# right align \newline \#right text \newline \newline \# If we put all three strings together with their formatting options, we would have a width of 60 to place the left, center, and right string variables. \newline print(f"\{left : \textless{}20\}\{center : \textasciicircum{}20\}\{right : \textgreater{}20\}") \newline \#left text center text! right text} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}