下面两行都会打印出12:
print int(12)
print int("012")
为什么下面打印出 10?
print int(012)
最佳答案
这是一个 octal (base 8)数字字面量,它在 Python 2 中原生支持(尽管令人困惑)以及十六进制(例如 0x1C
)。但是,默认情况下,所有数字都以十进制打印,因此您得到返回给您的整数的十进制(以 10 为基数)版本:
(1 * 8) + (2 * 1) = 10
Python 2 输出
$ python2 -c "print 012"
10
python 3
备注 Python 3 this has changed :
$ python3 -c "print(010)"
File "<string>", line 1
print(010)
^
SyntaxError: invalid token
关于python-2.7 - 以 0 开头的数字上的 int() 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637832/