Converting from strings to integers

Ken Seehof kseehof at neuralintegrator.com
Thu Aug 2 00:49:07 EDT 2001


From: "Stephen Smith" <ssmith619 at hotmail.com>

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

Hmm.  Works for me.  Try pasting a snippet from your python interpreter
when posting a question like that.

I'm guessing, but maybe you were expecting int(x) to change x into
an integer, since you referred to int(x) as a command.  But int is a
function that -returns- an integer.  The variable x is not changed.

>>> x = '87'
>>> int(x)
87
>>> x
'87'
>>> int(x) * 2
174
>>> x * 2
'8787'

Spend some time playing around in the interpreter.  It's a fast way
to learn.

- Ken   kseehof at neuralintegrator.com
http://ww.neuralintegrator.com/kseehof







More information about the Python-list mailing list