Home >Backend Development >Python Tutorial >How to use int function in python
#int() function is used to convert a string or number into an integer. Next, this article will introduce you to the relevant knowledge of the int() function in python. Friends who are interested should take a look together
int(x, [base])
Function:
The function is to convert a number or base type string into an integer.
Function prototype:
int(x=0)
int(x, base=10), the default value of base is 10, which means that base is not specified value, the function treats x as decimal.
Applicable Python version:
Python2.x
Python3.x
Note:
1. x can be a number or a string, But after base is assigned, x can only be a string
2. When x is used as a string, it must be of base type, that is to say, when x becomes a number, it must be represented in base base
Code example:
1. When x is a number:
int(3.14) # 3 int(2e2) # 200 int(100, 2) # 出错,base 被赋值后函数只接收字符串
2. When x is a string:
int('23', 16) # 35 int('Pythontab', 8) # 出错,Pythontab不是个8进制数
3. The possible value range of base is 2~ 36, including all English letters (not case sensitive), F in hexadecimal represents 15, then G will represent 16 in decimal, and so on....Z represents in thirty hexadecimal 35
int('FZ', 16) # 出错,FZ不能用十六进制表示 int('FZ', 36) # 575
4. The string 0x can appear in hexadecimal and is regarded as a hexadecimal symbol. Similarly, 0b can appear in binary and is regarded as a number 0 and a letter. x
int('0x10', 16) # 16,0x是十六进制的符号 int('0x10', 17) # 出错,'0x10'中的 x 被视作英文字母 x int('0x10', 36) # 42804,36进制包含字母 x
The above is the detailed content of How to use int function in python. For more information, please follow other related articles on the PHP Chinese website!