Home / Python / Reference / Built-in functions

Python int() function

Convert a value to a whole number.

Syntax

int(x=0, base=10)

From a float it cuts off the decimal part (it does not round). From a string, the text must look like a whole number. base reads numbers written in binary, hex and so on.

Parameters

xa number or a string
basethe base of a string number (2–36)

Return value

int

Example

Code

print(int("42") + 1)
print(int(7.9), int(-7.9))     # cuts towards zero
print(round(7.9))              # to round instead
print(int("  12 "))            # spaces at the ends are allowed
print(int("1010", 2), int("ff", 16))

Output

43
7 -7
8
12
10 255
Good to know. int("3.5") and int("abc") raise ValueError. Use int(float("3.5")) for the first.

Related

Learn it step by step in the free lesson Values, Variables & Numbers, with exercises that check your answers.