int() Integer constructor
Last updated: 2026-09-22
int() Integer constructor
int() converts a number or string to int; strings can specify base.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
int(x[, base])
⚙️ Parameters
| x Required | A number or string to convert to int. |
|---|---|
| base Optional | Base (2 to 36); only valid when x is a string; defaults to decimal.(Default:None) |
Returns:An int: numbers are truncated/rounded, strings parsed in the given base; 0 when called with no args.
💥 Raises
ValueError— Raised when the string is malformed or base is outside 2-36.TypeError— Raised when the argument type cannot be converted to int.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
3
42
255
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does int(3.99) return? Is it 4?
AIt returns 3. int() truncates a float toward zero (drops the fractional part) rather than rounding; use round(3.99) to round.
QIs there a difference between int('42') and int(42)?
ABoth give 42. But int('42') parses a string (and can take a base argument), while int(42) returns a number as-is. int('4.5') raises a ValueError — a string cannot contain a decimal point.
QWhat does int('ff', 16) return?
AIt returns 255. The base argument sets the parse radix (2-36); going out of range raises a ValueError, and base only applies when the first argument is a string.
QWhat does int() return with no arguments?
AIt returns 0.