|
311 | 311 | %
|
312 | 312 | %
|
313 | 313 | \hsection{Escaping Characters}%
|
| 314 | +\label{sec:str:escaping}% |
| 315 | +\pythonIdx{str!escaping}\pythonIdx{escaping}% |
314 | 316 | %
|
315 | 317 | \noviceHint{%
|
316 | 318 | This section tells you how to include special characters in strings that you otherwise could not include, like quotation marks. %
|
|
320 | 322 | \begin{figure}%
|
321 | 323 | \centering%
|
322 | 324 | \includegraphics[width=0.8\linewidth]{\currentDir/strEscapes}%
|
323 |
| -\caption{Escaping special characters in \python\ strings.}% |
| 325 | +\caption{Escaping special characters in \python\ strings.\pythonIdx{str!escaping}\pythonIdx{escaping}}% |
324 | 326 | \label{fig:strEscapes}%
|
325 | 327 | \end{figure}%
|
326 | 328 | %
|
|
331 | 333 | Well, you can say, if I need a double quotation mark, then I will delimit my string with single quotation marks and vice versa.
|
332 | 334 | This is all good as long as you do not need \emph{both} single and double quotation marks inside the string.
|
333 | 335 |
|
334 |
| -The answer to this problem is \emph{escaping}\pythonIdx{str!escaping}. |
| 336 | +The answer to this problem is \emph{escaping}\pythonIdx{str!escaping}\pythonIdx{escaping}. |
335 | 337 | The idea is very simple:
|
336 | 338 | If we need a certain quotation mark, then we simply put a backslash~(\inQuotes{\textbackslash})\pythonIdx{\textbackslash} before it.
|
337 | 339 | The backslash tells the \python\ interpreter that the next character should be considered as a normal character and not be interpreted as any special character, like a string delimiter.
|
338 | 340 |
|
339 |
| -In \cref{fig:strEscapes}, we present several strings with escape sequences. |
| 341 | +In \cref{fig:strEscapes}, we present several strings with escape sequences\pythonIdx{str!escaping}\pythonIdx{escaping}. |
340 | 342 | For clarity reasons, we pass them to the \pythonilIdx{print} function to output them, which means that they show up undelimited in the console.
|
341 | 343 | A double quotation mark can be printed as \pythonil{print("\\"")}, i.e., as a string which is delimited and that then contains the escape sequence {\textbackslash\textquotedbl}\pythonIdx{\textbackslash\textquotedbl}.
|
342 | 344 | It then shows up as~\textil{"} in the output.
|
343 | 345 | A single quotation mark can be printed as \pythonil{print("\\'")}, i.e., via the escape sequence {\textbackslash\textquotesingle}\pythonIdx{\textbackslash\textquotesingle}.
|
344 | 346 | It appears as~\textil{'} in the output.
|
345 | 347 |
|
346 | 348 | If the use the backslash character~\inQuotes{\textbackslash}\pythonIdx{\textbackslash} to escape characters which may otherwise have some special meaning {\dots} then what do we do if we need a backslash inside of a string?
|
347 |
| -Easy: We escape it. |
| 349 | +Easy: We escape it\pythonIdx{str!escaping}\pythonIdx{escaping}. |
348 | 350 | The escape sequence \inQuotes{\textbackslash\textbackslash}\pythonIdx{\textbackslash\textbackslash} is converted to a single backslash and \expandafter\pythonil{print("\\\\")} writes \textil{\\} to the output.
|
349 | 351 | Knowing these sequences, we can now try to \pythonil{print("\\"\\'\\\\")}.
|
350 | 352 | The result printed to the output then is~\textil{"'\\}.
|
351 | 353 |
|
352 |
| -Another situation where escape sequences are nice is when we want to have strings that span over multiple lines. |
| 354 | +Another situation where escape sequences are nice is when we want to have strings that span over multiple lines\pythonIdx{str!escaping}\pythonIdx{escaping}. |
353 | 355 | The newline sequence \inQuotes{\textbackslash{n}}\pythonIdx{\textbackslash{n}} represents a \inQuotes{newline character} which causes the console to skip to the next line.
|
354 | 356 | \pythonil{print("Hello\\nWorld!")} will first print \textil{Hello}, then end the current line and begin a new line, and then print \textil{World!}.
|
355 | 357 | Notice that the newline character sequence \inQuotes{\textbackslash{n}} is used in \linux\ and similar systems, whereas \windows\ uses \inQuotes{\textbackslash{r}\textbackslash{n}}\pythonIdx{\textbackslash{r}\textbackslash{n}}.
|
|
361 | 363 | If you want to include a horizontal tabulator in a string, the escape sequence \inQuotes{\textbackslash{t}}\pythonIdx{\textbackslash{t}} is your friend:
|
362 | 364 | \pythonil{print("The horizontal tab is like a bigger space: '\\t'.")} yields \inlinelistingbox{\texttt{The~horizontal~tab~is~like~a~bigger~space:~'~~~~'.}}.
|
363 | 365 |
|
364 |
| -Finally, a backslash can also escape an actual newline in your string. |
| 366 | +Finally, a backslash can also escape an actual newline in your string\pythonIdx{str!escaping}\pythonIdx{escaping}. |
365 | 367 | If you have a string that is too long to write on a single line but you do not want to have a linebreak inside the actual string, you can simply put a backslash, hit \keys{\enter}, and continue the typing the string.
|
366 | 368 | The linebreak will then be ignored entirely.
|
367 | 369 | Therefore, if you print~\pythonil{print("Hello\\}, hit \keys{\enter}, and then continue to write \pythonil{World!")}, this produces the output~\textil{HelloWorld!}.
|
368 | 370 |
|
369 | 371 | Escape sequences allow us to write arbitrary text in strings.
|
370 | 372 | We already learned the sequences \inQuotes{\textbraceleft\textbraceleft}\pythonIdx{\textbraceleft\textbraceleft} and \inQuotes{\textbraceright\textbraceright}\pythonIdx{\textbraceright\textbraceright} that were designed for \pglspl{fstring} only.
|
371 | 373 | The backslash-based escape sequence we discussed in this section work for both \pglspl{fstring} and normal strings.%
|
| 374 | +\pythonIdx{str!escaping}\pythonIdx{escaping}% |
372 | 375 | \endhsection%
|
373 | 376 | %
|
374 | 377 | \hsection{Multi-Line Strings}%
|
|
0 commit comments