|
170 | 170 | \begin{figure}%
|
171 | 171 | \centering%
|
172 | 172 | \includegraphics[width=0.8\linewidth]{\currentDir/fstrings}%
|
173 |
| -\caption{\python\ f-strings in action.}% |
| 173 | +\caption{\python\ \pglspl{fstring} in action.}% |
174 | 174 | \label{fig:fstrings}%
|
175 | 175 | \end{figure}
|
176 | 176 |
|
177 | 177 | \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{"}. |
179 | 179 | 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.%
|
180 | 180 | %
|
181 | 181 | \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{ |
183 | 183 | The code that formats my inline \python\ examples sometimes eats spaces after \textbraceleft\ or \textbraceright. %
|
184 | 184 | Therefore, some of the strings presented here may look a bit off. %
|
185 | 185 | In \cref{fig:fstrings}, they are printed correctly, though.}.
|
|
278 | 278 | It will be interpreted as a single \inQuotes{\textbraceright}~brace.
|
279 | 279 | \pythonil{f"Single braces without expression: \{\{ and \}\}."} simply becomes \pythonil{"Single braces without expression: \{ and \}."}
|
280 | 280 |
|
| 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 | + |
281 | 291 | You are now able to convert the results of your computations to nice text.%
|
282 | 292 | \endhsection%
|
283 | 293 | %
|
|
0 commit comments