Is there the same function in python as atoi() in C

Alex Martelli aleax at aleax.it
Tue Aug 5 08:00:14 EDT 2003


<posted & mailed>

Robbie wrote:

> Someone tell me,
> if not exist, how to write one with the same function?
> I have made a try, but there is no character type in python,
> so i failed, someone help me,
> thanks very much

I see somebody already suggested string.atoi(astring, base)
[with the base defaulting to 10], but a simpler way is to
just call int(astring, base) [again, base defaults to 10] --
int is a built-in, and all that function atoi in module
string does is actually to delegate right on to int.

Alas, we don't have such a generalized-base "itoa" -- only the
built-ins str(i) [base 10], oct(i) [base 8], hex(i) [base 16].


Alex





More information about the Python-list mailing list