...
 
Commits (2)
\begin{module}[id=python-fstring-literals]
\importmhmodule[path=python/en/string-literals]{python-string-literals}
\lstset{language=python,aboveskip=2pt,belowskip=0pt}
\begin{frame}[fragile]
\frametitle{Formatted String Literals (aka. f-strings)}
\begin{itemize}
\item
\begin{definition}
\Defiiis[name=f-string]{formatted}{string}{literal} (aka. \defis{f-string}) are
\trefiis[python-string-literals]{string}{literal} can contain $\pythonLanguage$ expressions
that will be replaced with their values at runtime.
\Defis{f-string} are prefixed by a prefix \lstinline|f| or \lstinline|F|, the
expressions are delimited by curly braces, and \lstinline|{|/\lstinline|}| are
represented by \lstinline|{{|/\lstinline|}}|.
\end{definition}
\item
\begin{example}[title=An f-String for IWGS]
\lstinputmhlisting{python/ex/f-string.py}
\end{example}
\item
\begin{example}[title=An f-String with Dictionary]
\lstinputmhlisting{python/ex/f-dict.py}
Note that we alternated the quotes here to avoid the following problems:
\lstinputmhlisting{python/ex/f-dict-error.py}
\end{example}
\end{itemize}
\end{frame}
\end{module}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
% LocalWords: python-fstring-literals importmhmodule Defiiis defis trefiis
% LocalWords: lstinputmhlisting
\begin{module}[id=python-string-literals]
\importmhmodule[path=python/en/unicode]{python-unicode}
\lstset{language=python,aboveskip=2pt,belowskip=0pt}
\begin{frame}[fragile]
\frametitle{String Literals in $\pythonLanguage$}
\begin{itemize}
\item
\begin{omtext}[title=Problem]
How to write strings including special characters?
\end{omtext}
\item
\begin{definition}
$\pythonLanguage$ uses \defiis{string}{literal}, i.e character sequences surrounded
by one, two, or three sets of matched single or double quotes for string input. The
content can contain \defiis{escape}{sequence}, i.e. the \defii{escape}{character}
backslash followed by a code character for problematic characters:
\begin{center}
\begin{tabular}{|l|l||l|l|}\hline
Seq & Meaning & Seq& Meaning\\\hline\hline{}
\lstinline|\\| & Backslash (\textbackslash) & \lstinline|\'| & Single quote (') \\\hline
\lstinline|\"| & Double quote (") & \lstinline|\a| & Bell (BEL) \\\hline
\lstinline|\b| & Backspace (BS) & \lstinline|\f| & Form-feed (FF)\\\hline
\lstinline|\n| & Linefeed (LF) & \lstinline|\r| & Carriage Return (CR)\\\hline
\lstinline|\t| & Horizontal Tab (TAB) & \lstinline|\v| & Vertical Tab (VT) \\\hline
\end{tabular}
\end{center}
In triple-quoted \trefiis{string}{literal}, unescaped newlines and quotes are honored, except
that three unescaped quotes in a row terminate the literal.
Prefixing a \trefii{string}{literal} with a \lstinline|r| or \lstinline|R| turns it
into a \defiii{raw}{string}{literal}, in which backslashes have no special meaning.
\end{definition}
\item
\begin{omtext}[title=Note]
using the backslash as an \trefii{escape}{character} forces us to escape it as well.
\end{omtext}
\item
\begin{example}
The string \lstinline|"a\nb\nc"| has length five and three lines, but the string
\lstinline|r"a\nb\nc"| only has length seven and only one line.
\end{example}
\end{itemize}
\end{frame}
\end{module}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
% LocalWords: importmhmodule hline Seq trefiis honored trefii defiii
>>> f'The course {course['name']} has {course['students']} students.'
File "<stdin>", line 1
f'The course {course['name']} has {course['students']} students.'
^
SyntaxError: invalid syntax
>>> course = {'name'="IWGS",students='66'}
>>> f"The {course['name']} course has {course['students']} students."
'The IWGS course has 66 students.'
>>> course="IWGS"
>>> f"The {course} course has {6*11} students"
'The IWGS course has 66 students'
......@@ -7,6 +7,8 @@
\mhinputref{python/en/comments}
\mhinputref{python/en/variables}
\mhinputref{python/en/datatypes}
\mhinputref{python/en/string-literals}
\mhinputref{python/en/fstring-literals}
\mhinputref{python/en/branchloop}
\nmhinputref{python/snip/python-branchloop}
\mhinputref{python/en/branching}
......