89DEVs

Python int() function

int() overview

The int() function converts the given value into a number of type integer. It can be used to convert a float type value to an integer. The values are never rounded up, instead all values are rounded down to the next integer. The int() function can also be used to convert a number in an integer to a real integer value.

float values and int()

The int() function can be used to convert a float value to an integer. # int() on float values number = 12.55 myInt = int(number) print(myInt) The float value 12.55 is convert to the integer value 12. It is not rounded up, instead it is rounded down to 12. 12

string values and int()

If a string contains a number, it can be converted to an integer value by using the int() function. # int() on string values string = '123' myInt = int(string) print(myInt) The string value '123' is converted to the integer value 123. 123

syntax of int()

The syntax of the int() function is: int(item=0, base=10)

arguments of int()

The int() function accepts two optional arguments. The first one is the value to be converted to an integer, if no value is set the default value of 0 is returned. The second argument is the base of the value and its default value is 10.

return values of int()

The int() function returns an integer value based on the arguments passed to it.

                
        

Summary


Click to jump to section