Converting from strings to integers
Deon Garrett
garrett at cs.colostate.edu
Wed Aug 1 23:27:36 EDT 2001
On 1 Aug 2001 17:49:31 -0700, Stephen Smith <ssmith619 at hotmail.com> wrote:
> Hi, I'm new to Python and am just trying out an example of file I/O.
> I am trying to read a file (successfully), and then multiply a value
> by another static variable. The value from the file was outputting in
> the 'string' format, and I need to convert it into an integer. I
> tried using the int(x) command, but it doesn't seem to care, it is
> still in the string format. The number is a plain integer (87, for
> example). How on earth do you convert the value to an integer?
> Thanks for your help!
use the "atoi" function of the string module.
>>> s = "87"
>>> type (s)
<type 'str'>
>>> import string
>>> i = string.atoi (s)
>>> type (i)
<type 'int'>
There is also an atof function in the same module that converts to
floats and an atol to convert to Python Long ints.
More information about the Python-list
mailing list