Skip to main content
Engineering LibreTexts

4.3: Type Conversion Functions

  • Page ID
  • Python also provides built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:

    >>> int('32')
    >>> int('Hello')
    ValueError: invalid literal for int() with base 10: 'Hello'

    int can convert floating-point values to integers, but it doesn't round off; it chops off the fraction part:

    >>> int(3.99999)
    >>> int(-2.3)

    float converts integers and strings to floating-point numbers:

    >>> float(32)
    >>> float('3.14159')

    Finally, str converts its argument to a string:

    >>> str(32)
    >>> str(3.14159)
    • Was this article helpful?