Problems with conversion of values in strings to integers

Jørgen Cederberg jorgencederberg at hotmail.com
Mon Oct 6 07:38:33 EDT 2003


Hi,

using Python 2.2.1 on Windows and QNX I'm having trouble understandig 
why int() raises a ValueError exception upon converting strings.

 >>> int('-10')
-10
 >>> int('10')
10
 >>> int(10.1)
10
 >>> int('10.1')
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 10.1

This is quite frustating. The only solution I can see is to do:
 >>> int(float('10.1'))
10

Is this the only solution or are there any other way to do this?

Regards
Jorgen Cederberg





More information about the Python-list mailing list