Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

int: integers and data conversion

int creates an integer from a suitable number or text. Converting a floating-point value truncates its fractional part towards zero; it does not round to the nearest integer.

Version: Python 3.14

Syntax

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

With no argument, int() produces 0. int(" 12 ") accepts surrounding whitespace, but int("3.9") raises ValueError: that text is not an integer representation. In contrast, int(-3.9) produces -3 rather than -4. This is truncation, not the floor operation.

For text you can specify a base: int("ff", 16) produces 255. Valid bases are 2 through 36, or 0 to recognize allowed prefixes such as 0x. Base 0 does not treat every leading zero as ordinary decimal notation. An explicit base requires textual or byte input, not a floating-point number.

Invalid text raises ValueError, and an unsupported type can raise TypeError. Infinity cannot be converted to int: it raises OverflowError. After conversion, also check domain constraints, such as whether an age or quantity is permitted.

Python integers have no fixed width, but available memory and time still limit them. Conversions involving very long representations may be subject to the interpreter’s digit limit. Do not disable that protection for untrusted input merely to hide an error.

Examples

print(int(" 12 "), int("ff", 16), int(-3.9))
try:
    int("3.9")
except ValueError:
    print("ValueError")
try:
    int(float("inf"))
except OverflowError:
    print("OverflowError")

Result

12 255 -3
ValueError
OverflowError

Sources