Python Tutorial Python Advanced Python References Python Libraries

Python int() Function



The Python int() function converts different data types into an integer. Datatypes that can be used with this function are an integer literal, a float literal and a string literal (provided the string represents a whole number). This function has an optional parameter which can be used to specify base of the number when converting from a string literal. Default value of the optional parameter is 10 (decimal number).

Syntax

int(literal, base)

Parameters

literal Required. Value of an integer, float or string literal.
base Optional. Specify base of the number when converting from a string literal. default value is 10 (decimal).

Converting different literals into integer data type

In the example below, int() function is used to convert integer literal, float literal and string literal into an integer data type.

x = int(1)
print(x)

y = int(10.55)
print(y)

z = int('100')
print(z)

The output of the above code will be:

1
10
100

Converting binary string literals into integer data type

In the example below, int() function is used to convert a binary string literal into an integer data type.

x = int('100', 2)
print(x)

The output of the above code will be:

4

❮ Python Built-in Functions