python中int函数
Python int()函数 (Python int() function)
int() functionis used to convert a string (that should contain a number/integer), number (integer, float) into an integer value.
int()函数用于将字符串(应包含数字/整数),数字(整数,浮点数)转换为整数值。
Syntax: 句法:int(value, [base=10])
Parameter: 参数: -
value – source value (string, number(integer/float)) to be converted in an integer value.
value –要转换为整数值的源值(字符串,数字(整数/浮点数))。
-
base – it’s an optional parameter with default 10, it is used to define the base of source value, for example: if source string contains a binary value then we need to use base 2 to convert it into an integer.
base –这是一个可选参数,默认值为10,用于定义源值的基数,例如:如果源字符串包含二进制值,则需要使用基数2将其转换为整数。
int – returns an integer value.
返回值:int –返回一个整数值。
Example: 例:Input:
a = "1001"
print(int(a))
Output:
1001
Input:
a = 10.23
print(int(a))
Output:
10
Input:
a = "1110111"
print(int(a,2))
Output:
119
Python code to convert values to an integer Python代码将值转换为整数 # python code to demonstrate example
# of int() function
a = 10.20
b = "1001"
c = "000"
print("a: ", a)
print("int(a): ", int(a))
print("b: ", b)
print("int(b): ", int(b))
print("c: ", c)
print("int(c): ", int(c))
Output 输出量 a: 10.2
int(a): 10
b: 1001
int(b): 1001
c: 000
int(c): 0
Python code to convert different numbers systems (binary, octal and hexadecimal) to integer value (decimal) Python代码可将不同的数字系统(二进制,八进制和十六进制)转换为整数值(十进制) # python code to demonstrate example
# of int() function
a = "10101010"
print("a: ", int(a,2))
a = "1234567"
print("a: ", int(a,8))
a = "5ABC12"
print("a: ", int(a,16))
Output 输出量 a: 170
a: 342391
a: 5946386
翻译自: https://www.includehelp.com/python/int-function-with-example.aspx
python中int函数