Exercise 3: Convert String to Float
Convert a string to a float.
We'll cover the following
Problem statement
Determine which method can be used to turn a string, such as "1.5"
, into a floating-point number, such as 1.5
. Remember, floats and integers are different kinds of numbers. Confirm that you’ve found the right methods by trying them in the widget below.
Note: Assign your solution to the
result
variable to test the code.
Assume that the input string only contains data that can be safely converted to a floating-point number.
Example
input_string = "1"
result = 1.0
input_string = ""
result = 0.0
input_string = "2.2"
result = 2.2
Try it yourself
def string_to_num(input_string)result = ""# Start your code herereturn resultend