Python int() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, int(x) converts x to an integer. A float is truncated toward zero (int(3.9) is 3, int(-3.9) is -3), and a string of digits is parsed (int("42") is 42). int() with no argument returns 0.

For strings you can give a base from 2 to 36, such as 2 for binary or 16 for hex, or base=0 to read a prefix like 0x. Python ints have no size limit; they grow as large as memory allows.

int is a built-in type (a class) that you call like a function: each call creates a new int object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Convert a number, or parse a string in a chosen base.

python
n = int(x)
n = int(text, base)
CallWhat it does
int(-3.9)Returns -3 (truncates toward zero).
int(" 42 ")Returns 42; surrounding whitespace is allowed.
int("ff", 16)Returns 255.
int("0b101", 0)Returns 5; base 0 reads the prefix.

Underscores are allowed between digits, as in source code: int("1_000") is 1000.

Library declarations
python
int(x=0)
int(x, base=10)
# class int: int is a built-in type

Parameters

ParameterDescription
xOptional. A number, a string, bytes, or an object with __int__() or __index__(). Default 0.
baseOptional. 0 or 2 to 36. Only allowed when x is a string, bytes, or bytearray. Default 10.

Return Value

Return ValueDescription
intThe integer value. Floats are truncated, not rounded.

Python Examples

Example 1 — Convert Text Input to Numbers

Parse strings with spaces, signs, and underscores and add them up.

python
raw_values = ["42", "  -7 ", "+15", "1_000"]

total = 0
for raw in raw_values:
    number = int(raw)
    total += number
    print(repr(raw), "->", number)
print("total:", total)

How It Works

  1. Text to number. Each string is parsed into a real int that supports arithmetic.
  2. Whitespace and signs. Leading and trailing spaces are ignored, and + or - signs are accepted.
  3. Digit separators. "1_000" is read as one thousand.
  4. repr() shows the input. Printing repr(raw) keeps the quotes and spaces visible: 42 - 7 + 15 + 1000 = 1050.

Example 2 — Truncation vs Rounding vs Floor

Compare int() with round() and math.floor() for positive and negative floats.

python
import math

for x in [3.9, -3.9, 2.5]:
    print(x, "int:", int(x), "round:", round(x), "floor:", math.floor(x))

print(int(True), int(False))
print(int(7 / 2), 7 // 2)

How It Works

  1. Toward zero. int() just drops the fraction: 3.9 becomes 3 and -3.9 becomes -3.
  2. Floor goes down. math.floor(-3.9) is -4, so floor and int() differ only for negatives.
  3. Nearest. round() picks the nearest integer, and 2.5 goes to the even 2.
  4. Booleans and division. True converts to 1; int(7 / 2) and 7 // 2 both give 3 for positive numbers.

Example 3 — Parse Other Bases and Reject Bad Input

Read binary, octal, hex, and base-36 text, then handle strings that are not whole numbers.

python
print(int("ff", 16))
print(int("777", 8))
print(int("1010", 2))
print(int("0x1f", 0))
print(int("z", 36))

for text in ["12", "3.5", "abc"]:
    try:
        print(int(text))
    except ValueError:
        print(f"{text!r} is not a whole number")

How It Works

  1. Any base to 36. Hex ff is 255, octal 777 is 511, binary 1010 is 10.
  2. Base 0. int("0x1f", 0) sees the 0x prefix and reads 1 * 16 + 15 = 31.
  3. Letters as digits. Base 36 uses 0-9 and a-z, so "z" is 35.
  4. ValueError for bad text. A decimal point or letters in base 10 make int() raise ValueError, which the loop reports.

Frequently Asked Questions

Call int("42"). It accepts surrounding whitespace, a sign, and underscores, and raises ValueError for anything else.
No, it truncates toward zero: int(3.9) is 3 and int(-3.9) is -3. Use round() for the nearest integer.
int("3.5") raises ValueError. Use int(float("3.5")) to get 3, or round(float("3.5")) to get 4.
Pass the base: int("ff", 16) is 255 and int("1010", 2) is 10. Use base 0 to detect prefixes like 0x and 0b.
No. Python ints grow without a fixed limit. sys.maxsize is only the largest index a list can use, not the largest int.

Next: isinstance()

Check whether an object is an instance of a class.

isinstance() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful