Python int() Function

Short answer
int(x) converts a number or a numeric string to an integer, cutting off any decimal part; int(text, base) reads text in another number base.

You reach for int() whenever a value that should be a whole number is stored as something else: text typed by a user, a value read from a file, or a float from a calculation. It is the main tool in type casting.

int() is strict about text. Only strings that look like whole numbers are accepted, which is the source of the common ValueError. Converting a decimal string needs float() first.

Quick facts
Syntaxint(x=0) or int(x, base=10)
Parametersx: a number, or a string/bytes holding an integer; base: 2 to 36 or 0, allowed only when x is a string
ReturnsAn int; int() with no argument returns 0
RaisesValueError for text that is not an integer, TypeError for unsupported types such as None

How int() converts floats, strings and booleans

A float is truncated toward zero, not rounded: int(3.9) is 3 and int(-3.9) is -3. For rounding use round(). A string may contain a sign, surrounding whitespace and underscores between digits, so int(" 42 ") and int("1_000") both work. A boolean converts to 1 or 0.

Python
print(int(3.9), int(-3.9), round(3.9))
print(int(" 42 "), int("-7"), int("1_000"))
print(int(True), int())

Converting with a base: binary, octal and hex strings

Pass the base as the second argument to read text written in another number system. int("1010", 2) is 10 and int("ff", 16) is 255. With base=0, Python follows literal prefixes, so int("0x1f", 0) is 31. A base can only be given when the first argument is a string.

Python
print(int("1010", 2))
print(int("ff", 16))
print(int("0x1f", 0))

int() vs float() and round() for numeric strings

To turn '3.7' into a whole number, go through float() first.
Inputint()float()round(float())
'42'4242.042
'3.7'ValueError3.74
3.733.74
-3.7-3-3.7-4
' 8 '88.08
'abc'ValueErrorValueErrorValueError

Very long digit strings

Since Python 3.11 converting a string with more than 4300 digits raises ValueError, a safety limit against denial-of-service attacks. Normal programs never hit it. See how to convert a string to int for the full recipe including error handling.

Examples you can run

Press Run, then edit the code and run it again.

Convert user input to an integer

input() always returns text, so wrap it in int() before doing arithmetic.

Convert user input to an integer
age = int(input("Age: "))
print("Next year:", age + 1)
Expected outputAge: 19
Next year: 20

Truncation versus rounding

int() drops the decimals; it never rounds.

Truncation versus rounding
price = 49.99
print(int(price))
print(round(price))
print(int(-2.5))
Expected output49
50
-2

Safely convert text that may be invalid

Catch ValueError when the text may not be a number.

Safely convert text that may be invalid
for text in ["12", "3.5", "abc"]:
    try:
        print(int(text))
    except ValueError:
        print("not an integer:", text)
Expected output12
not an integer: 3.5
not an integer: abc

Common mistakes

Converting a decimal string directly

✗ Wrong

print(int("3.7"))
ValueError: invalid literal for int() with base 10: '3.7'

✓ Right

print(int(float("3.7")))

int() rejects text with a decimal point. Convert with float() first, then int() if you want to drop the decimals.

Converting None

✗ Wrong

value = None
print(int(value))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

✓ Right

value = None
print(int(value or 0))

Check for None before converting, or give a default value.

Passing a base with a number

✗ Wrong

print(int(10, 2))
TypeError: int() can't convert non-string with explicit base

✓ Right

print(int("10", 2))

A base is only valid with text. Pass "10" instead of 10.

Python int() Function: questions people ask

How do I convert a string to an integer in Python?

Use int(text), for example int('42') gives 42. The text must be a whole number, otherwise Python raises ValueError.

Does int() round or truncate?

It truncates toward zero, so int(2.9) is 2 and int(-2.9) is -2. Use round() if you want the nearest integer.

Why does int('3.5') raise an error?

Because '3.5' is not an integer literal. Use int(float('3.5')), which gives 3.

Keep learning

Step-by-step lessons:
Python CastingPython Numbers

Related:
How to Convert a String to an Int in PythonPython float() FunctionPython round() FunctionHow to Convert a Float to an Int in PythonPython input() Function

More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython float() FunctionPython str() FunctionPython input() FunctionPython sum() Function See all →