In Python, int(x) converts x to an integer. A float is truncated toward zero (int(3.9) is 3, int(-3.9) is -3), and a string of digits is parsed (int("42") is 42). int() with no argument returns 0.
For strings you can give a base from 2 to 36, such as 2 for binary or 16 for hex, or base=0 to read a prefix like 0x. Python ints have no size limit; they grow as large as memory allows.
int is a built-in type (a class) that you call like a function: each call creates a new int object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
Convert a number, or parse a string in a chosen base.
python
n = int(x)
n = int(text, base)
Call
What it does
int(-3.9)
Returns -3 (truncates toward zero).
int(" 42 ")
Returns 42; surrounding whitespace is allowed.
int("ff", 16)
Returns 255.
int("0b101", 0)
Returns 5; base 0 reads the prefix.
Underscores are allowed between digits, as in source code: int("1_000") is 1000.
Library declarations
python
int(x=0)
int(x, base=10)
# class int: int is a built-in type
Parameters
Parameter
Description
x
Optional. A number, a string, bytes, or an object with __int__() or __index__(). Default 0.
base
Optional. 0 or 2 to 36. Only allowed when x is a string, bytes, or bytearray. Default 10.
Return Value
Return Value
Description
int
The integer value. Floats are truncated, not rounded.
Python Examples
Example 1 — Convert Text Input to Numbers
Parse strings with spaces, signs, and underscores and add them up.
python
raw_values = ["42", " -7 ", "+15", "1_000"]
total = 0
for raw in raw_values:
number = int(raw)
total += number
print(repr(raw), "->", number)
print("total:", total)
Toward zero.int() just drops the fraction: 3.9 becomes 3 and -3.9 becomes -3.
Floor goes down.math.floor(-3.9) is -4, so floor and int() differ only for negatives.
Nearest.round() picks the nearest integer, and 2.5 goes to the even 2.
Booleans and division.True converts to 1; int(7 / 2) and 7 // 2 both give 3 for positive numbers.
Example 3 — Parse Other Bases and Reject Bad Input
Read binary, octal, hex, and base-36 text, then handle strings that are not whole numbers.
python
print(int("ff", 16))
print(int("777", 8))
print(int("1010", 2))
print(int("0x1f", 0))
print(int("z", 36))
for text in ["12", "3.5", "abc"]:
try:
print(int(text))
except ValueError:
print(f"{text!r} is not a whole number")
Output
255
511
10
31
35
12
'3.5' is not a whole number
'abc' is not a whole number
How It Works
Any base to 36. Hex ff is 255, octal 777 is 511, binary 1010 is 10.
Base 0.int("0x1f", 0) sees the 0x prefix and reads 1 * 16 + 15 = 31.
Letters as digits. Base 36 uses 0-9 and a-z, so "z" is 35.
ValueError for bad text. A decimal point or letters in base 10 make int() raise ValueError, which the loop reports.
Frequently Asked Questions
Call int("42"). It accepts surrounding whitespace, a sign, and underscores, and raises ValueError for anything else.
No, it truncates toward zero: int(3.9) is 3 and int(-3.9) is -3. Use round() for the nearest integer.
int("3.5") raises ValueError. Use int(float("3.5")) to get 3, or round(float("3.5")) to get 4.
Pass the base: int("ff", 16) is 255 and int("1010", 2) is 10. Use base 0 to detect prefixes like 0x and 0b.
No. Python ints grow without a fixed limit. sys.maxsize is only the largest index a list can use, not the largest int.