Converting to Integers with int()
float() converts integers and numeric strings to floating-point numbers, adding decimal representation where none existed.
Try it: Type conversion: int(), float(), str(), bool()
Exactly how int(), int(text, base), float(), str() and bool() turn a value into another type: Python's real parsing rules for text (whitespace, sign, underscores, 0x/0o/0b prefixes, inf/nan, exponents) and its truthiness rules.
How it works
- int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
- int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
- float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
- int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
- bool() is truthiness: '', 0, 0.0 and None are False; every non-empty string — even '0' or 'False' — is True.
Default run (8 steps): int(' -0x_1F ', 0): the argument is a str of 10 characters. … int(' -0x_1F ', 0) → -31
Simplified: The argument is a Python literal typed as text (ASCII only; escapes \t \n \r). Unicode digits and bytes arguments are not modelled.
Loading the simulation…
A Decimal Does Not Round Itself
Suppose a calculation produces 3.9, but the next step requires an integer. Passing the value to int() does not ask Python to choose the nearest whole number. Instead, int() removes the decimal portion. The result is 3. This same rule applies to larger decimal portions: 3.99999 becomes 3, not 4.
What do you think happens?
What values will int() produce for 3.9, -3.9, and 7.0?
Reveal answer
3, -3, and 7
int() truncates by removing the decimal portion. It does not round to the nearest integer, so 3.9 becomes 3 and -3.9 becomes -3.
What int() Changes
int() converts values to integers by truncating their decimal portions. For a floating-point input, the digits after the decimal point are removed entirely. The size of the removed portion does not change the rule: both 3.1 and 3.99999 become 3. A whole-number floating-point value such as 7.0 becomes the integer 7.
3
-3
7
3Which Inputs Are Accepted
The source identifies two important input categories for int(). Floating-point values can be converted, and strings that represent whole numbers can be converted. A string containing a decimal representation, such as '3.14', cannot be passed directly to int(); that input raises an error. Invalid input also raises an error.
12
12
12
raises an errorConverting a Decimal String
You receive the string '3.14' and need an integer.
- 1Direct conversion
int('3.14')raises an error because the string represents a decimal number rather than a whole number. - 2Convert to floating point
float('3.14')converts the numeric string to a floating-point value. - 3Convert to integer
int(float('3.14'))then truncates the decimal portion.
int(float('3.14')) produces 3.
int(), float(), and str()
These conversion functions do different jobs. int() produces an integer and removes a decimal portion when the input has one. float() produces a floating-point value and preserves fractional values. The source also distinguishes both numeric conversions from str(), which is a separate conversion choice rather than an operation that applies int()'s truncation rule.
| Function | Result described in the source | Effect on 3.9 |
|---|---|---|
| int() | Integer; decimal portion is truncated | 3 |
| float() | Floating-point value; fractional value is preserved | 3.9 |
| str() | A separate conversion choice from numeric conversion | Use when a string representation is required |
Choose the function according to the kind of result needed.
Mistakes Beginners Make
Common mistake Expecting int() to round to the nearest integer.
What it looks likeint(3.9) is expected to be 4.Why it failsint()truncates by removing the decimal portion; it does not round.Do this insteadExpect int(3.9) to produce 3.Common mistake Assuming the size of the decimal portion changes the result.
What it looks likeint(3.99999) is expected to be 4 because the value is close to 4.Why it failsThe truncation rule removes the decimal portion regardless of its magnitude.
Do this insteadExpect int(3.99999) to produce 3.Common mistake Passing a decimal string directly to int().
What it looks likeint('3.14')Why it failsint()works with whole-number strings, but a decimal string raises an error.Do this insteadUse int(float('3.14')) when the intended process is to convert the decimal string and then truncate it.
Common mistake Using float() when an integer result is required.
What it looks likefloat('12')Why it failsfloat()returns a floating-point value rather than an integer.Do this insteadUse int('12') when the input is a whole-number string and an integer result is required.
Check Your Prediction
Predict the result of each expression before checking it: int(8.75), int(-8.75), int('8'), int('8.75'), and int(float('8.75')). For each one, decide whether the result is an integer or an error, and explain what happened to the decimal portion.
Hints
- For a floating-point input,
int()removes the decimal portion. - A string containing only a whole number can be converted directly.
- A decimal string needs
float()beforeint()if truncation is intended.
Key Takeaways
- 1
int()converts supported values to integers. - 2For floating-point inputs,
int()truncates by removing the decimal portion rather than rounding. - 3
int()can convert floats and whole-number strings, but decimal strings and invalid input raise an error. - 4To convert a decimal string to an integer through truncation, use
int(float('3.14')). - 5
float()preserves fractional values in a floating-point result, whilestr()represents a separate conversion choice.
At a glance
int()removes decimal portions; it does not round.- Values such as 3.9, -3.9, and 7.0 become 3, -3, and 7.
- Floating-point values and whole-number strings can be converted with
int(). - Decimal strings cannot be passed directly to
int(); convert them withfloat()first when truncation is wanted. float()preserves fractions, so it behaves differently fromint().