Exercise 3: Convert String to Float

Convert a string to a float.

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.


input_string = "1"
result = 1.0

input_string = ""
result = 0.0

input_string = "2.2"
result = 2.2

Try it yourself

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy