Built-in Functions and Types

int()

Construct an integer from a numeric value or integer text. Float conversion discards the fractional part toward zero, not toward negative infinity. Text can contain a sign, surrounding whitespace and correctly placed underscores. Explicit base must be 0 or 2 through 36; base 0 detects supported literal prefixes and rejects nonzero unprefixed forms with leading zeros. Invalid text raises ValueError; infinities raise OverflowError and NaN raises ValueError. A base cannot accompany a numeric input.

Formal Syntax & Structure

python syntaxpython
int(number=0, /)int(string, /, base=10)

Parameters & Attributes

NameTypeDescription
numbernumber or conversion-protocol objectNumeric form; omitted input gives zero.
stringstr | bytes | bytearrayText form; required when a base is supplied (alternative to number).
baseint or object supporting __index__Radix for text, default 10; 0 infers the prefix.

Return Value

int

An integer value; the original object is not modified.

Working Example

Example Snippetpython
print(int(-8.9))print(int("10101", 2))print(int("0x2a", 0))
Python Reference