int()
Turns text like "42" or a decimal number like 3.9 into a whole number, and can read binary, hexadecimal and other bases too.
int(x=0)
int(x, base=10)int() makes a whole number (an integer). Give it a string of digits, such as "42", and you get the number 42 back, ready for arithmetic. Give it a decimal number and it drops everything after the point.
You need it all the time with input(), files and CSV data, because they always give you text. "7" + "5" is "75", while int("7") + int("5") is 12.
The surprise: int() does not round, it cuts. int(3.99) is 3 and int(-3.99) is -3: it always moves toward zero. If you want the nearest whole number, use round(). Also, int("3.5") fails: a string must hold a whole number.
Parameters
| Parameter | What it is |
|---|---|
xoptional, default 0 | A string holding a whole number (spaces around it are fine, and so are _ between digits), a float, or a bool. With nothing, int() gives 0. |
baseoptional, default 10 | The number system the string is written in: 2 for binary, 8 for octal, 16 for hexadecimal, anything from 2 to 36, or 0 to read the base from a prefix such as 0x. Only allowed when x is a string. |
Return value
An int. Python integers have no size limit, so they can be as large as you need.
Examples
1Text to a whole number
a = "7"
b = "5"
print(a + b)
print(int(a) + int(b))
print(int(" 42\n"))
print(int("1_000_000"))75 12 42 1000000
Spaces and new lines around the digits are ignored, and underscores between digits are allowed.
2Decimals are cut, not rounded
print(int(3.99))
print(int(-3.99))
print(round(3.99))3 -3 4
int() always moves toward zero. round() goes to the nearest whole number.
3Read binary and hexadecimal
print(int("1010", 2))
print(int("ff", 16))
print(int("0x1F", 16))
print(int("777", 8))10 255 31 511
4Add up quantities from a CSV line
line = "pens,12,notebooks,5,folders,8"
parts = line.split(",")
quantities = [int(q) for q in parts[1::2]]
print(quantities)
print("Total items:", sum(quantities))[12, 5, 8] Total items: 25
parts[1::2] takes every second piece, starting from position 1: the numbers.
5Turn a price into cents
Storing money as whole cents avoids rounding surprises. But watch what happens with a plain int():
price = 19.99
print(price * 100)
print(int(price * 100))
print(int(round(price * 100)))1998.9999999999998 1998 1999
19.99 cannot be stored exactly in binary, so 19.99 * 100 lands a hair below 1999 and int() alone loses a cent. round() first is the safe habit.
Common mistakes
Converting a decimal string directly
int("3.5") raises ValueError, because the string is not a whole number. Go through float() first.
try:
print(int("3.5"))
except ValueError:
print("ValueError: '3.5' is not a whole number")
print(int(float("3.5")))ValueError: '3.5' is not a whole number 3
Forgetting that int() cuts toward zero
int(4.35 * 100) gives 434, not 435, because the multiplication lands a hair below 435. Use round() when you want the nearest whole number.
print(4.35 * 100)
print(int(4.35 * 100))
print(round(4.35 * 100))434.99999999999994 434 435
Questions people ask
How do I convert a string to an int in Python?
int("42"). If the text may not be a number, wrap it in try / except ValueError.
How do I round a float to the nearest integer?
Use round(): round(2.7) gives 3. int(2.7) gives 2, because it cuts.
How do I check whether a string is a whole number?
text.isdigit() works for positive numbers without signs. For anything else, try int(text) and catch ValueError.
How do I convert an int to binary or hex?
Use bin(10) ('0b1010'), hex(255) ('0xff'), or an f-string: f"{255:x}" gives ff.