Conversion: atoi(), atof(), atol()

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 ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.