Python int() Function
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.
| Syntax | int(x=0) or int(x, base=10) |
|---|---|
| Parameters | x: a number, or a string/bytes holding an integer; base: 2 to 36 or 0, allowed only when x is a string |
| Returns | An int; int() with no argument returns 0 |
| Raises | ValueError 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.
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.
print(int("1010", 2))
print(int("ff", 16))
print(int("0x1f", 0))int() vs float() and round() for numeric strings
| Input | int() | float() | round(float()) |
|---|---|---|---|
'42' | 42 | 42.0 | 42 |
'3.7' | ValueError | 3.7 | 4 |
3.7 | 3 | 3.7 | 4 |
-3.7 | -3 | -3.7 | -4 |
' 8 ' | 8 | 8.0 | 8 |
'abc' | ValueError | ValueError | ValueError |
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.
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.
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.
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"))✓ 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))✓ 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))✓ 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 →