PyDocs

Nav

Python Runtime Services

int

  • int(number=0, /)
  • int(string, /, base=10)
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating-point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer
iteral.
>>> int('0b100', base=0)
4
Changed in 3.4, 3.6, 3.7, 3.8, 3.11, 3.14
  • 3.4: If base is not an instance of int and the base object has a base.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.
  • 3.6: Grouping digits with underscores as in code literals is allowed.
  • 3.7: The first parameter is now positional-only.
  • 3.8: Falls back to __index__() if __int__() is not defined.
  • 3.11: int string inputs and string representations can be limited to help avoid denial of service attacks. A ValueError is raised when the limit is exceeded while converting a string to an int or when converting an int into a string would exceed the limit. See the integer string conversion length limitation documentation.
  • 3.14: int() no longer delegates to the __trunc__() method.
Members of int

a

as_integer_ratio
Return a pair of integers, whose ratio is equal to the original int.

b

bit_count
Number of ones in the binary representation of the absolute value of self.
bit_length
Number of bits necessary to represent self in binary.

f

from_bytes
Return the integer represented by the given array of bytes.

i

is_integer
Returns True.

t

to_bytes
Return an array of bytes representing an integer.
1 Example
>>> int(123.45)
123
>>> int('123')
123
>>> int('   -12_345\n')
-12345
>>> int('FACE', 16)
64206
>>> int('0xface', 0)
64206
>>> int('01110011', base=2)
115
from the Python docs
See Also
No see-alsos for builtins/int
0 Notes
No notes for int