Python int() Function Guide
You can convert data to an integer by using the int() function. This function can convert other types of data, such as strings, floating-point numbers, into integers.
For example, converting the string “123” to an integer:
num_str = "123"
num_int = int(num_str)
print(num_int) # 输出:123
When converting a floating point number to an integer, the int() function will simply discard the decimal part and keep only the integer part. For example:
num_float = 3.14
num_int = int(num_float)
print(num_int) # 输出:3
It’s important to note that if the data to be converted cannot be represented as an integer (such as a string containing non-numeric characters), it will raise a ValueError error.