The Python int()
function is a built-in function that converts a specified value into an integer number.
For example, if you have a string, x
, the int()
function would return an integer object constructed from the x
string.
int(value, base)
value
: A number or string to be converted to integer object.
base
: Optional, with default value 10. Number format.
How to convert a string into an int
:
value= '187'print('converting string 187 into an int:')print int(value)string = '10'print('printing string 10 with base 2,4,8 and 16:')print(int(string, 2))print(int(string, 4))print(int(string, 8))print(int(string, 16))string = '100'print('printing string 100 with base 2,4,8 and 16:')print(int(string, 2))print(int(string, 4))print(int(string, 8))print(int(string, 16))
How to convert a float into an int
:
print('converting float 133.5 into an int:')x = int(133.5)print(x)