Section 2.10 Type Conversion Functions
The problem at hand is that the input
function always gives us a string. We need the ability to convert the string into an integer so we can do arithmetic operations on it.
Python provides a few functions that will allow us to convert from one data type to another. 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 int
function can take a floating point number or a string and turn it into an integer. 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
.
Now we can ask the user for a number with input
, take the resulting string, convert it to a number, and then do arithmetic with it. Here’s the program from the last page, fixed. This time, if you enter 7468 for the number of seconds, the program won’t crash:
In line 3, we’ve given the result of input
the name total_seconds_str
to remind ourselves that input
always gives us a string, and we use the int
function in line 4 to convert it to an integer.
Python has other type converter functions in addition to int
. The float
type converter function can turn an integer, a float, or a syntactically legal string into a float.
The type converter function str
turns its argument into a string. Remember that when we print a string, the quotes are removed. However, if we print the type, we can see that it is definitely a str
.
Let’s check your understanding of input
and type conversion.
Checkpoint 2.10.1.
What is the data type of variable n
after the following statements execute?
n = input("Please enter your age: ")
# user types in 18
print (type(n))
str (string)
- All input from users is read in as a string.
int (integer)
- Even though the user typed in an integer, it does not come into the program as an integer.
18
- 18 is the value of what the user typed, not the type of the data.
Checkpoint 2.10.2.
Click on all of the variables of type int
in the code below
seconds = input("Please enter the number of seconds you wish to convert")
hours = int(seconds) // 3600
total_secs = int(seconds)
secs_still_remaining = total_secs % 3600
print(secs_still_remaining)
Checkpoint 2.10.3.
Click on all of the variables of type str
in the code below
seconds = input("Please enter the number of seconds you wish to convert")
hours = int(seconds) // 3600
total_secs = int(seconds)
secs_still_remaining = total_secs % 3600
print(secs_still_remaining)
Checkpoint 2.10.4.
What value is printed when the following statement executes?
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.