int()
int(x=0) / int(x, base=10) int · Added in v3.0 · Updated March 13, 2026 · Built-in Functions The int() function converts a number or string to an integer. It is one of Python’s most frequently used built-in functions, essential for type conversion, parsing user input, and working with numerical data. When given a float, int() truncates toward zero (it does not round).
Syntax
int(x=0)
int(x, base=10)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | int | float | str | bytes | bytearray | 0 | A number, string, or byte-like object to convert to an integer. Defaults to 0 if omitted. |
base | int | 10 | The base (radix) for string conversion. Must be between 2 and 36. Only valid when x is a string. |
Returns: An integer object. If x is already an int, returns x unchanged (not a copy).
Examples
Converting from float
When converting from a float, int() truncates toward zero rather than rounding:
# Float truncation (toward zero)
print(int(3.7))
# 3
print(int(-3.7))
# -3
# Note: this is NOT rounding
print(int(3.5))
# 3
print(int(3.999))
# 3
Converting from string
Strings can be converted to integers. Leading and trailing whitespace is ignored:
# Basic string to int
print(int("42"))
# 42
# With whitespace
print(int(" 100 "))
# 100
# Negative numbers in strings
print(int("-273"))
# -273
Converting with different bases
Use the base parameter to parse strings in different numeral systems:
# Binary (base 2)
print(int("1010", 2))
# 10
# Octal (base 8)
print(int("755", 8))
# 493
# Hexadecimal (base 16)
print(int("FF", 16))
# 255
# Case-insensitive for bases > 10
print(int("ff", 16))
# 255
# Base 36 (uses 0-9 and a-z)
print(int("z", 36))
# 35
print(int("hello", 36))
# 1836316
Converting from bytes
Bytes and bytearray objects can be converted using a specific encoding:
# Bytes to int
print(int(b"42"))
# 42
Using the default value
When called with no arguments, int() returns 0:
# No argument defaults to 0
print(int())
# 0
Boolean conversion
Booleans are a subclass of int, but int() explicitly converts them:
# True becomes 1, False becomes 0
print(int(True))
# 1
print(int(False))
# 0
Common Patterns
Parsing user input
User input always comes as a string—use int() to convert it:
# Simple age parser
user_input = "25"
age = int(user_input)
print(age + 1)
# 26
# Handling potential errors
def safe_int_parse(value):
try:
return int(value)
except ValueError:
return None
print(safe_int_parse("42"))
# 42
print(safe_int_parse("not a number"))
# None
Working with file data
When reading numerical data from files, convert strings to integers:
# Simulated file content
lines = ["100", "200", "300"]
numbers = [int(line) for line in lines]
print(sum(numbers))
# 600
Base conversion utilities
Create your own base conversion helpers:
def from_base(encoded, base):
return int(encoded, base)
def to_base(number, base):
if number == 0:
return "0"
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
result = []
while number > 0:
result.append(digits[number % base])
number //= base
return "".join(reversed(result))
print(from_base("FF", 16))
# 255
print(to_base(255, 16))
# ff
Rounding vs truncation
Remember that int() truncates, not rounds. For rounding, use round():
# int() truncates
print(int(3.9))
# 3
# round() rounds to nearest
print(round(3.9))
# 4
print(round(3.5))
# 4
print(round(3.499))
# 3
Errors
ValueError: invalid literal for int()
# Non-numeric string
int("3.14")
# ValueError: invalid literal for int() with base 10: '3.14'
# Use float() first
int(float("3.14"))
# 3
ValueError: int() base must be >= 2
# Invalid base
int("42", 1)
# ValueError: int() base must be >= 2
# Valid bases are 2-36
int("42", 2) # OK
int("42", 36) # OK
TypeError: int() argument must be a string
# Lists cannot be converted directly
int([42])
# TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
# For containers, extract the value first
int(["42"][0])
# 42
See Also
- built-in::float — converts to floating-point number
- str::str — converts to string
- built-in::round — rounds numbers (not truncates)
- built-in::bool — converts to boolean
- built-in::ord — converts a character to its integer code point
- built-in::chr — converts an integer to its character representation