Skip to content

Commit 60df85e

Browse files
committed
improved f-strings
1 parent 8187128 commit 60df85e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed
Binary file not shown.

text/main/basics/simpleDataTypesAndOperations/str/str.tex

+13-3
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,16 @@
170170
\begin{figure}%
171171
\centering%
172172
\includegraphics[width=0.8\linewidth]{\currentDir/fstrings}%
173-
\caption{\python\ f-strings in action.}%
173+
\caption{\python\ \pglspl{fstring} in action.}%
174174
\label{fig:fstrings}%
175175
\end{figure}
176176

177177
\pythonIdx{str!f}\pythonIdx{f-string}Let us therefore discuss a very powerful and much more convenient gadget in \python's string processing toolbox: format strings, or \pglspl{fstring}\pythonIdx{f-string}\pythonIdx{str!f} for short~\cite{PSF2024FSL,PEP498,M2017WAFSIPAHCIUT,B2023PFS}.
178-
An f-string is like a normal string, except that it starts with \pythonil{f"}\pythonIdx{f\textquotedbl\idxdots\textquotedbl} instead of \pythonil{"}.
178+
An \pgls{fstring} is like a normal string, except that it starts with \pythonil{f"}\pythonIdx{f\textquotedbl\idxdots\textquotedbl} instead of \pythonil{"}.
179179
And, most importantly, it can contain other data and even complete expressions inside curly braces~(\pythonil{\{...\}}\pythonIdx{\textbraceleft\idxdots\textbraceright!f-string}) which are then embedded into the string.%
180180
%
181181
\begin{sloppypar}%
182-
In \cref{fig:fstrings}, we first consider the f-string \pythonil{f"\{12345678901234\}\ is a really big integer."}\footnote{
182+
In \cref{fig:fstrings}, we first consider the \pgls{fstring} \pythonil{f"\{12345678901234\}\ is a really big integer."}\footnote{
183183
The code that formats my inline \python\ examples sometimes eats spaces after \textbraceleft\ or \textbraceright. %
184184
Therefore, some of the strings presented here may look a bit off. %
185185
In \cref{fig:fstrings}, they are printed correctly, though.}.
@@ -278,6 +278,16 @@
278278
It will be interpreted as a single \inQuotes{\textbraceright}~brace.
279279
\pythonil{f"Single braces without expression: \{\{ and \}\}."} simply becomes \pythonil{"Single braces without expression: \{ and \}."}
280280

281+
As final example, let us look at a very cool ability of \pglspl{fstring}\pythonIdx{f-string!=}.
282+
Often, we want to print an expression together with its result.
283+
Earlier, we wrote \pythonil{f"\{5\}\ + \{4\}\ = \{5 + 4\}"} is evaluated to \pythonil{"5 + 4 = 9"}.
284+
What we actually wanted to print was the expression \pythonil{5 + 4} together with its result.
285+
This can be done much easier:
286+
We can simply write \pythonil{f"\{5\ + 4\ = \}"}, which, too is evaluated to \pythonil{"5 + 4 = 9"}
287+
The more complex \pythonil{f"\{23\ *\ sin(2\ -\ 5)\ =\ \}"} becomes \pythonil{"23\ *\ sin(2\ -\ 5)\ =\ -3.245760185376946"}.
288+
One cool feature of this kind of expression-to-string conversation is that you can add the other format specifiers we discussed earlier after the \pythonil{=}.
289+
For example, you could write \pythonil{f"\{23\ *\ sin(2\ -\ 5)\ =\ :.2f\}"} and then the \pythonil{.2f} format would be applied to the result of the expression, i.e., you would get \pythonil{"23 * sin(2 - 5) = -3.25"} as the result of the extrapolation.
290+
281291
You are now able to convert the results of your computations to nice text.%
282292
\endhsection%
283293
%

text/main/basics/variables/assignment/assignment.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
We can use \pythonil{int_var} just like any other value.
118118
For example, we can compute \pythonil{2 + int_var} and pass the result to the \pythonilIdx{print} function.
119119
This will then print \pythonil{3} to the \pgls{stdout} of our program.
120-
We can also use \pythonil{int_var} in \pglspl{fstring}\pythonIdx{f-string}\pythonIdx{str!f} about which we leared back in \cref{sec:fstrings}.
120+
We can also use \pythonil{int_var} in \pglspl{fstring}\pythonIdx{f-string}\pythonIdx{str!f} about which we learned back in \cref{sec:fstrings}.
121121
\pythonil{f"int_var has value \{int_var\}."} will render to \pythonil{"int_var has value 1."}.%
122122
\end{sloppypar}%
123123
%

0 commit comments

Comments
 (0)