String Manipulation and Conversion
The conversion path is int(float('3.14')).
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 Up
Suppose a program receives the value 7.9 but needs an integer. Applying int() does not ask whether 7.9 is closer to 7 or 8. Instead, int() removes the decimal portion. The result is 7. This behavior is called truncation.
What do you think happens?
What values result when int() is applied to 7.9 and -7.9?
Reveal answer
7 and -7
int() removes the decimal portion from each value. It does not round to the nearest integer.
Following the Truncation Step
Truncation removes the decimal part entirely, regardless of how large that decimal part is. Therefore, 3.99999 becomes 3, not 4. The same removal rule applies to a negative decimal such as -7.9, producing -7 rather than -8. The operation is not nearest-integer rounding.
Tracking the Decimal Portion
Determine the result of int(3.99999).
- 1Start with the value
The value is 3.99999, which has an integer portion of 3 and a decimal portion of .99999.
- 2Apply int()
int()removes the decimal portion instead of rounding based on its size. - 3Read the result
The remaining integer portion is 3.
int(3.99999) produces 3, not 4.
Accepted and Rejected Inputs
int() works with integer values, floating-point values, and strings that contain whole numbers. It raises an error when given a string containing a decimal value or other invalid input. The important distinction is between a decimal number and a decimal string: a decimal number can be truncated directly, while a string that includes decimal notation cannot be passed directly to int().
| Input category | int() behavior |
|---|---|
| Integer value | Converts to an integer |
| Decimal number | Removes the decimal portion |
| Whole-number string | Converts to an integer |
| Decimal string | Raises an error |
| Invalid input | Raises an error |
The input's form determines whether int() succeeds and whether truncation is needed.
Converting Decimal Text
A string containing a decimal, such as the text 3.14, cannot be converted directly to an integer with int(). To convert that decimal string, first use float() and then pass the resulting value to int(). The conversion path is int(float('3.14')). The inner float() conversion handles the decimal string; the outer int() conversion then truncates the decimal portion.
Reading int(float('3.14'))
Explain the conversion sequence in int(float('3.14')).
- 1Read the inner conversion
float('3.14')converts the decimal string into a floating-point value. - 2Read the outer conversion
int()receives that decimal value and removes its decimal portion. - 3State the result
The final integer is 3.
int(float('3.14')) produces 3.
Comparing Conversion Paths
These functions represent different conversion intentions. int() produces an integer and, for decimal numbers, removes the decimal portion. float() is the intermediate conversion used in the source example to interpret the decimal string 3.14 before int() is applied. str() belongs to the string-conversion side of the comparison rather than the truncation step. When the goal is specifically to turn decimal text into an integer, the relevant path is float() followed by int().
Mistakes to Avoid
Common mistake Assuming int() rounds to the nearest integer
What it looks likeExpecting int(3.99999) to produce 4Why it failsint()truncates by removing the decimal portion, regardless of its magnitude.Do this insteadTreat int(3.99999) as 3.Common mistake Treating a decimal string like a decimal number
What it looks likePassing '3.14' directly to int()Why it failsint()works with whole-number strings, but a decimal string is an input form that raises an error.Do this insteadUse int(float('3.14')) when converting the decimal string described in the source.
Common mistake Ignoring the order of nested conversions
What it looks likeReading int(float('3.14')) as one unexplained operationWhy it failsThe inner
float()conversion handles the decimal string first, and the outerint()conversion performs truncation afterward.Do this insteadTrace the expression from the inside out: decimal string to float, then float to integer.
Before using int(), identify both the input's form and the behavior you want. If the input is a decimal number, expect truncation rather than rounding. If the input is decimal text, use the two-step conversion shown in the source: first float(), then int().
Check Your Prediction
For each expression, predict whether it produces an integer result or raises an error. Then explain whether truncation occurs: int(8), int(8.75), int('8'), int('8.75'), and int(float('8.75')).
Hints
- Separate numeric values from strings.
- A whole-number string is different from a decimal string.
- For nested conversion, evaluate the inner
float()step before the outerint()step. - Remember that
int()removes the decimal portion rather than rounding.
- 1
int()converts values to integers by truncating decimal portions instead of rounding. It can handle integer values, decimal numbers, and whole-number strings. A decimal string or other invalid input raises an error. To convert decimal text such as '3.14' to an integer, first usefloat()and thenint().
Key Takeaways
int()removes the decimal portion rather than rounding.- Values such as 3.99999 become 3.
int()accepts decimal numbers and whole-number strings, but decimal strings and invalid input raise an error.- A decimal string can be converted through
int(float('3.14')). - The order of nested conversions matters:
float()handles the decimal string beforeint()truncates the result.