int()
Parse strings into integers — any base from 2 to 36, strict about what counts as a number.
Common call
int("42")
Returns
int — arbitrary precision, no overflow
Replaces
int("ff", 16) → 255; prefixes like 0x are accepted when they match
Watch out
int("12.5") raises — parse via float() first if decimals may appear
int(xx — The value to convert. Strings may have surrounding whitespace, a sign, and underscores between digits. Floats truncate toward zero.type: str | number · required, basebase — 2–36, or 0 to infer from a 0x/0o/0b prefix. Only valid for string inputs.type: int · default: 10=10)
→ int
Demo
Live evaluation
Try:
Inputs
stringstrthe string to parse
baseint2-36, empty = 10
Output
int('42')
42
Whitespace and a sign are fine; underscores between digits too. Everything else must be a digit valid in the base — which is why ’12.5’ raises exactly Python’s ValueError. With base 16, letters a–f become digits, with or without the 0x prefix.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| x | str | number | yes | The value to convert. Strings may have surrounding whitespace, a sign, and underscores between digits. Floats truncate toward zero. |
| base | int | no (10) | 2–36, or 0 to infer from a 0x/0o/0b prefix. Only valid for string inputs. |
Return value
int — The parsed integer. Invalid strings raise ValueError; base applies to string inputs only.
Common patterns
Safe user-input parsing
try/except beats pre-validation — it handles every edge at once.
try: n = int(raw) except ValueError: n = DEFAULT
Hex / binary conversions
The base argument reads what f-strings and bin()/hex() write.
color = int("ff8800", 16) # 16746496 flags = int("1011", 2) # 11
Truncate a float toward zero
int() drops the fraction — it does not round.
int(3.99) # 3 int(-3.99) # -3
Examples
1. Parse decimal
int("42")
Returns
422. Parse hex
int("ff", 16)
Returns
2553. Whitespace and sign OK
int(" -42 ")
Returns
-424. Underscores allowed
int("1_000_000")
Returns
1000000Pitfalls
1. Decimal strings raise
int() parses integers only — "12.5" is not one.
Raises
int("12.5")
ValueError: invalid literal for int() with base 10: '12.5'
Fix
int(float("12.5"))
12
2. int() truncates, round() rounds
Converting floats with int() always chops toward zero.
Not rounding
int(3.99)
3
When rounding is meant
round(3.99)
4
3. Leading zeros are fine — base 0 is the strict one
int("010") is 10, but base 0 mimics literal rules and rejects it.
Raises
int("010", 0)
ValueError: invalid literal for int() with base 0: '010'
Plain base 10
int("010")
10
When to use
Use it
- Parsing integer input (with try/except)
- Hex/octal/binary string conversion
- Truncating floats toward zero
Reach for something else
- Decimal strings possible → float() first
- Rounding semantics wanted → round()
- Validating without converting → str.isdecimal
Notes
Complexity
O(n) in the digit count; Python ints are arbitrary precision
Return
int — never overflows
CPython impl
Objects/longobject.c :: PyLong_FromString
Memory
Grows with magnitude — big ints are fine
Thread-safe
Yes — pure construction
FAQ
Parses like a Python literal: 0x → hex, 0o → octal, 0b → binary, no prefix → decimal — and rejects leading zeros like 010, exactly as source code would.
History
3.6
Underscores in numeric strings accepted (PEP 515).
3.0
int and long unified into one arbitrary-precision type.