Concepts / String Manipulation and Conversion

String Manipulation and Conversion

The conversion path is int(float('3.14')).

  • Programming
Interactive lab

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

  1. int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
  2. int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
  3. float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
  4. int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
  5. 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.

Educational simulation

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?

  • 8 and -8

  • 7 and -7

  • 7 and -8

Reveal answer
Answer:

7 and -7

int() removes the decimal portion from each value. It does not round to the nearest integer.

Following the Truncation Step

7.97-7.9-7int()int()
What changes when int() removes the decimal portion from positive and negative values?

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.

Worked example

Tracking the Decimal Portion

Determine the result of int(3.99999).

  1. 1
    Start with the value

    The value is 3.99999, which has an integer portion of 3 and a decimal portion of .99999.

  2. 2
    Apply int()

    int() removes the decimal portion instead of rounding based on its size.

  3. 3
    Read the result

    The remaining integer portion is 3.

Result

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 to int()IntegerInteger resultDecimal numberTruncated integerWhole-number stringErrorDecimal stringvalue typevalue typevalue typevalue typeaccepteddecimal removedacceptedrejected
What happens next when int() receives an integer, a decimal number, a whole-number string, or a decimal string?
Input categoryint() behavior
Integer valueConverts to an integer
Decimal numberRemoves the decimal portion
Whole-number stringConverts to an integer
Decimal stringRaises an error
Invalid inputRaises 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.

3.14decimal string3.14float value3integer resultfloat()int() truncates
How does a decimal string become an integer when direct int() conversion is not available?
Worked example

Reading int(float('3.14'))

Explain the conversion sequence in int(float('3.14')).

  1. 1
    Read the inner conversion

    float('3.14') converts the decimal string into a floating-point value.

  2. 2
    Read the outer conversion

    int() receives that decimal value and removes its decimal portion.

  3. 3
    State the result

    The final integer is 3.

Result

int(float('3.14')) produces 3.

Comparing Conversion Paths

int()3float()3.14str()textinteger conversiondecimal conversionstring conversion
How do the conversion functions differ when a decimal string must become an integer?

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 like
    Expecting int(3.99999) to produce 4
    Why it fails

    int() truncates by removing the decimal portion, regardless of its magnitude.

    Do this instead
    Treat int(3.99999) as 3.
  • Common mistake

    Treating a decimal string like a decimal number

    What it looks like
    Passing '3.14' directly to int()
    Why it fails

    int() works with whole-number strings, but a decimal string is an input form that raises an error.

    Do this instead

    Use int(float('3.14')) when converting the decimal string described in the source.

  • Common mistake

    Ignoring the order of nested conversions

    What it looks like
    Reading int(float('3.14')) as one unexplained operation
    Why it fails

    The inner float() conversion handles the decimal string first, and the outer int() conversion performs truncation afterward.

    Do this instead

    Trace the expression from the inside out: decimal string to float, then float to integer.

Best practice

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

MEDIUM

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 outer int() step.
  • Remember that int() removes the decimal portion rather than rounding.
  1. 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 use float() and then int().

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 before int() truncates the result.

Related concepts

More concepts starting with S