newbie str to int

James no at email.com
Tue Oct 2 06:22:29 CEST 2001


I am switching from Perl to Python, and would
like to convert string '123abc' to integer 123.

foo = int('123abc') # error
foo = string.atoi('123abc') #error
foo = eval('123abc') # error

I tried the above, then tried the following.

tmp = ''
for i in '123abc':
    if 47 < ord(i) < 58:
        tmp += i
foo = int(tmp) # yay

Why doing simple thing like this is so complicated ?

Perl
$foo = '123abc' + 0;

Please explain in newsgroup.
Many thanks in advance.






More information about the Python-list mailing list