英文文檔:
class int
(x=0) class int
(x, base=10)
Return an integer object constructed from a number or string x, or return 0
if no arguments are given. If x is a number, return x.__int__()
. For floating point numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string, bytes
, or bytearray
instance representing an integer literal in radix base. Optionally, the literal can be preceded by +
or -
(with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a
to z
(or A
to Z
) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b
/0B
, 0o
/0O
, or 0x
/0X
, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0)
is not legal, while int('010')
is, as well as int('010', 8)
.
說明:
1. 不傳入參數時,得到結果0。
>>> int()
0
2. 傳入數值時,調用其__int__()方法,浮點數將向下取整。
>>> int(3) 3 >>> int(3.6) 3
3. 傳入字符串時,默認以10進制進行轉換。
>>> int('36') 36 >>> int('3.6') Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> int('3.6') ValueError: invalid literal for int() with base 10: '3.6'
4. 字符串中允許包含"+"、"-"號,但是加減號與數值間不能有空格,數值后、符號前可出現空格。
>>> int('+36') 36 >>> int('-36') -36 >>> int(' -36 ') -36 >>> int(' - 36 ') Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> int(' - 36 ') ValueError: invalid literal for int() with base 10: ' - 36
5. 傳入字符串,並指定了進制,則按對應進制將字符串轉換成10進制整數。
>>> int('01',2) 1 >>> int('02',3) 2 >>> int('07',8) 7 >>> int('0f',16) 15