int
Convert a number or a string to an integer.
Python 3.13
int([x])
int(x, base=10)- [ ]optional part
- =default value
- x
- A number or a string with an integer; the fractional part of a number is dropped.int | float | strOptional
- base
- The number base from 2 to 36 or 0, 10 by default; only for a string or bytes.intOptional
Return value
An integer; 0 without arguments. Raises ValueError for a string that is not a valid number.
Examples
Integer from a string
Python 3.13
print(int('123'))123
Dropping the fractional part
Python 3.13
print(int(3.9), int(-3.9))3 -3
Hexadecimal number
Python 3.13
print(int('ff', 16))255
Details
The fractional part is dropped without rounding: int(3.9) gives 3. A string such as "3.5" does not work; convert it with float first. Spaces around the number and _ between digits are allowed. With base=0, the base is taken from the prefix: 0x for hexadecimal, 0o for octal, 0b for binary, no prefix for decimal. So int("0x1f", 0) gives 31, while a decimal number with a leading zero, such as "017", raises ValueError.
