🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

int()

AI & DATA SCIENCE // int

int() converts a compatible value — a numeric string, a float, or a bool — into an integer, or returns 0 when called with no arguments.

Syntax

int(x=0)
int(x, base=10)

Deep Dive Course

Called on a string, int() parses it as a whole number in the given base (base 10 by default), raising ValueError if the string isn't a valid integer literal. Called on a float, it truncates toward zero rather than rounding, so int(3.9) is 3, not 4. The two-argument form, int(string, base), is handy for parsing hexadecimal, octal, or binary text, e.g. converting the string 'ff' in base 16.

1Understanding int()

Called on a string, int() parses it as a whole number in the given base (base 10 by default), raising ValueError if the string isn't a valid integer literal. Called on a float, it truncates toward zero rather than rounding, so int(3.9) is 3, not 4. The two-argument form, int(string, base), is handy for parsing hexadecimal, octal, or binary text, e.g. converting the string 'ff' in base 16.

💡

int() truncates floats — it does not round them. Use round() first if you want conventional rounding behavior before converting to int.

editor.html
print(int("42"))
print(int(3.9))
print(int("ff", 16))
localhost:3000

2Practical Example

Here is a real-world application of int() showing how it is used in production Python code.

editor.html
user_input = "  128  "
try:
    quantity = int(user_input)
    print(f"Ordering {quantity} units")
except ValueError:
    print("Please enter a whole number")
localhost:3000

3Best Practices

Follow these guidelines when working with int():

1. Wrap int(user_input) in a try/except ValueError block when parsing untrusted or user-provided text

2. Use int(s, 0) to auto-detect a prefixed base like a hex or binary literal string

3. Prefer int(x) over integer division by 1 for truncating a float to a whole number — it's clearer intent

⚠️

Tip: int() truncates floats — it does not round them. Use round() first if you want conventional rounding behavior before converting to int.

editor.html
print(int("42"))
print(int(3.9))
print(int("ff", 16))
localhost:3000

Examples

Example 01Basic Usage
print(int("42"))
print(int(3.9))
print(int("ff", 16))
Example 02Advanced Example
user_input = "  128  "
try:
    quantity = int(user_input)
    print(f"Ordering {quantity} units")
except ValueError:
    print("Please enter a whole number")

Best Practices

  • Wrap int(user_input) in a try/except ValueError block when parsing untrusted or user-provided text
  • Use int(s, 0) to auto-detect a prefixed base like a hex or binary literal string
  • Prefer int(x) over integer division by 1 for truncating a float to a whole number — it's clearer intent

Interview Question

What happens when you call int() on the string '3.5', and how would you convert that string to an integer anyway?

Hint: int() parses whole-number literals only.

int('3.5') raises ValueError, because int() only parses strings that look like whole numbers — it doesn't handle a decimal point. To convert such a string, you go through float first: wrap the string in float(), then pass that result to int(), which truncates it down to 3.

Exercises

MediumPractice using int() in a real scenario.
View Solution
print(int("42"))
print(int(3.9))
print(int("ff", 16))

Frequently Asked Questions

What happens when you call int() on the string '3.5', and how would you convert that string to an integer anyway?

int('3.5') raises ValueError, because int() only parses strings that look like whole numbers — it doesn't handle a decimal point. To convert such a string, you go through float first: wrap the string in float(), then pass that result to int(), which truncates it down to 3.

Related Functions

float()str()round()