Skip to content

Commit 9cd45a2

Browse files
committed
added conversion from strings to other datatypes ... now that should really complete the section on strings
1 parent 15fa0c9 commit 9cd45a2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Binary file not shown.

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,35 @@
276276
You are now able to convert the results of your computations to nice text.%
277277
\endhsection%
278278
%
279+
\hsection{Converting Strings to other Datatypes}%
280+
\begin{figure}%
281+
\centering%
282+
\includegraphics[width=0.8\linewidth]{\currentDir/fromStr}%
283+
\caption{Converting strings to other datatypes.}%
284+
\label{fig:fromStr}%
285+
\end{figure}%
286+
%
287+
While converting the other datatypes to strings is important to produce output, converting strings to the other datatypes is also important.
288+
If our programs accept input from the console, the command line, or from text files, we need to somehow translate these input strings to whatever datatype we actually need.
289+
Luckily, the datatypes we have discussed so far conveniently provide functions for doing so, and these functions are named like the datatypes themselves.
290+
291+
\cref{fig:fromStr} shows that we can convert the string \pythonil{"1111"} to an integer \pythonil{1111} simply by passing it to the function \pythonilIdx{int}\pythonIdx{int!function}.
292+
If we want to convert hexadecimal-formatted text such as the string \pythonil{"0x1111"} instead, we need to tell the \pythonilIdx{int} function that the basis for conversion is~16.
293+
Doing \pythonil{int("0x1111", 16)} yields \pythonil{4369}, because~$1+16^1+16^2+16^3=1+16+256+4096=4369$.
294+
Similarly, if we have a string in binary annotation, we pass~2 as second parameter to \pythonilIdx{int}.
295+
Calling \pythonil{int("0b1111", 2)} returns~\pythonil{15}, because~$1+2+4+8=15$.
296+
297+
We can also convert strings to floating point numbers by using the \pythonilIdx{float}\pythonIdx{float!function} function.
298+
This works for scientific notation~(\pythonil{float("2.233e4")} yields \pythonil{22330.0}) as well as for \inQuotes{normal} floating point numbers~(\pythonil{float("0.1123")} yields the \pythonil{float} \pythonil{0.1123}).
299+
The \pythonilIdx{float} function also extends the special values that a floating point value can take one.
300+
Consequently, \pythonil{float("inf")} gives us~\pythonilIdx{inf} and \pythonil{float("nan")} returns~\pythonilIdx{nan}.
301+
302+
Finally, the function \pythonilIdx{bool}\pythonIdx{bool!function} converts the strings \pythonil{"True"} and \pythonil{"False"} to \pythonilIdx{True} and \pythonilIdx{False}, respectively.
303+
With this, you are also able to convert strings to data that you can use as input for your computations.%
304+
%
305+
\endhsection%
306+
%
307+
%
279308
\hsection{Escaping Characters}%
280309
%
281310
\begin{figure}%
@@ -426,6 +455,11 @@
426455
\pglspl{fstring} are the tool for that.
427456
They can render almost arbitrary data as nicely formatted strings and take care of things such as rounding or inserting thousand separators.
428457

458+
Converting strings to the other datatypes that we have discussed so far can, conveniently, be done by using functions of the same names:
459+
The function \pythonilIdx{int} converts its argument string to an instance of \pythonilIdx{int}.
460+
The function \pythonilIdx{float} converts its argument string to an instance of \pythonilIdx{float}.
461+
And the function \pythonilIdx{bool}, well, you guess it.
462+
429463
Sometimes we want to include characters in our strings that are dodgy.
430464
For example, if our string is delimited by \pythonil{"} marking its begin and end, inserting such a \pythonil{"} inside the string would be awkward.
431465
Indeed, the \python\ interpreter would think that it marks the end of the string and then confuse the \pythonil{"} marking the actual end as the beginning of a new string.

0 commit comments

Comments
 (0)