ScanSkill

int

int() function returns the specified value into an integer.

Syntax

int(*value, base*)

Here,

  • value: Optional. Can be of string, int, float, or long type.
  • base: Optional. Represent the number format. Default: 10

Note: If the value is float, the conversion truncates towards zero. And if the number is not a number

i.e if the base is given, then the number must be a string or Unicode object representing a literal base. The default base is 10. Values allowed are 0 and 2-36.

Examples

  • Convert octal, hex, and binary integers into a decimal integer
>>> int(0o10)
8
>>> int(0x10)
16
>>> int(0b10)
2
  • Convert using a base argument
>>> int('0101', 2)
5
>>> int('0101', 8)
65
>>> int('0101', 16)
257
>>> int('0101', 10)
101
  • Convert string or float into an integer
>>> int("99") # string
99
>>> int(3.14)
3
>>> int(3.14e10)
31400000000L
>>> int(-3.14)
-3
  • Convert numbers with unary operator into integer
>>> int('-100')
-100
>>> int('+100')
100
>>> int('100')
100