首先看一下它的帮助信息:
>>> help(string.atoi)
Help on function atoi in module string:
atoi(s, base=10)
atoi(s [,base]) -> int Return the integer represented by the string s in the given base, which defaults to 10. The string s must consist of one or more digits, possibly preceded by a sign. If base is 0, it is chosen from the leading characters of s, 0 for octal, 0x or 0X for hexadecimal. If base is 16, a preceding 0x or 0X is accepted.
大概意思是:输入一个s的字符串,然后指定它的进制(默认为十进制),最后输出十进制的整数。
今天上网发现网上好多误导大家的,说base字段是用来指定输出整数的进制的。其实这是不对,不信大家可以试验一下。
如果我们按照他的理论来,假如我输入一个‘10’的字符串,希望得到一个八进制的输出。那应该得到的结果为12 。
但是我们测试一下:
>>> string.atoi(‘10’,8)
8
>>>
python中得到的结果为8,并不是我们想要的12.这是为什么呢?
因为base字段不是用来指定输出整数的进制的,而是用来指定输入字符串的进制的。
如果我们把上边的例子理解为:输入一个八进制的字符串‘10’,希望得到一个十进制的输出,那结果就为8了。
所以希望大家在这里不要混淆。