2.3. Type conversion functions

Sometimes it is necessary to convert values from one type to another. Python provides a few simple functions that will allow us to do that. The functions int, float and str will (attempt to) convert their arguments into types int, float and str respectively. We call these type conversion functions.

The type converter str turns its argument into a string. Recall that the print function requires its input to be string types. By using the str function it is possible to convert a number to a string before using it as input to the print function.

Note

Note that just like in math equations parentheses determine the order of execution. Inner parenthesis are executed first. So print(str(17)) will first convert 17 to a string, then it will print it.

The type converter float can turn an integer, a float, or a syntactically legal string into a float.

The int function can take a floating point number or a string, and turn it into an int. For floating point numbers, it discards the decimal portion of the number - a process we call truncation towards zero on the number line. Let us see this in action:

The last case shows that a string has to be a syntactically legal number, otherwise you’ll get one of those pesky runtime errors. Modify the example by deleting the bottles and rerun the program. You should see the integer 23.

Check your understanding

    data-3-1: What value is printed when the following statement executes?

    print(int(53.285))
    
  • Nothing is printed. It generates a runtime error.
  • The statement is valid Python code. It calls the int function on 53.285 and then prints the value that is returned.
  • 53
  • The int function truncates all values after the decimal and prints the integer value.
  • 54
  • When converting to an integer, the int function does not round up.
  • 53.285
  • The int function removes the fractional part of 53.285 and returns an integer, which is then printed.

    data-3-2: What value is printed when the following statement executes?

    print( int(53.785) )
    
  • Nothing is printed. It generates a runtime error.
  • The statement is valid Python code. It calls the int function on 53.785 and then prints the value that is returned.
  • 53
  • The int function truncates all values after the decimal and prints the integer value.
  • 54
  • When converting to an integer, the int function does not round.
  • 53.785
  • The int function removes the fractional part of 53.785 and returns an integer, which is then printed.

    data-3-3: What value is printed when the following statement executes?

    print(int(-53.785))
    
  • Nothing is printed. It generates a runtime error.
  • The statement is valid Python code. It calls the int function on -53.785 and then prints the value that is returned.
  • -53
  • The int function truncates all values after the decimal and prints the integer value.
  • -54
  • When converting to an integer, the int function does not round.
  • -53.785
  • The int function removes the fractional part of -53.785 and returns an integer, which is then printed.
You have attempted of activities on this page
Next Section - 2.4. Variables