# 将字符串转换为整数
string_number = "123"
integer_number = int(string_number)
print(integer_number) # 输出: 123
# 将浮点数转换为整数(会舍弃小数部分)
float_number = 123.456
integer_number = int(float_number)
print(integer_number) # 输出: 123
# 带基数的转换,例如将二进制、八进制、十六进制字符串转换为整数
binary_number = "1010"
integer_number = int(binary_number, 2)
print(integer_number) # 输出: 10
octal_number = "17"
integer_number = int(octal_number, 8)
print(integer_number) # 输出: 15
hexadecimal_number = "1a"
integer_number = int(hexadecimal_number, 16)
print(integer_number) # 输出: 26
int()
函数在 Python 中用于将其他类型的值转换为整数。它可以接受一个字符串或浮点数作为参数,并返回相应的整数值。此外,int()
还可以接受第二个参数来指定字符串的基数(例如二进制、八进制、十六进制等)。
上一篇:python爬虫框架
下一篇:zip在python中的含义
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站