June 2002
Beginner to intermediate
640 pages
14h 24m
English
atoi(s[,base]) converts a string into an integer. The default is decimal, but you can specify octal 8, hexadecimal 16, or decimal 10. If 0 is the base, the string will be parsed as a hexadecimal if it has a leading 0x and as an octal if it has a leading 0. Otherwise, it will be treated as a decimal.
Let's do an example. In this and all other examples in this chapter, you have to first import the string module: from string import *.
Convert "1" to an integer.
>>> atoi("1")
1
Convert "255" to a base 10 integer.
>>> atoi("255",10)
255
Convert "FF" to a base 16 integer.
>>> atoi("FF",16)
255
The atof(s) function converts a string to a float.
>>> atof("1.1")
1.1
The atol(s[, base]) converts a string to a ...
Read now
Unlock full access