Python Reference Library →

Python float() Function – Convert to Floating Point Number

Let’s look at float() function – convert to floating point number

  1. Notice

    float() converts a supported number or numeric string into a floating-point number.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = '3.5'
number = float(text)
print(number)
Live state

Top-level scope

text→Object 1
number→Object 2
Object 1 · str'3.5'Object 2 · float3.5
This step

float() converts a supported number or numeric string into a floating-point number.

Picture float() function – convert to floating point number

float() converts a supported number or numeric string into a floating-point number.

Image brief

Alt text
A diagram for Python float() Function – Convert to Floating Point Number. Instructions: text = '3.5' number = float(text) print(number). Displayed result: 3.5. The final example bindings are text --> object 1 (str); number --> object 2 (float). object 1: '3.5'; object 2: 3.5
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python float() Function – Convert to Floating Point Number". Teaching focus: float() converts a supported number or numeric string into a floating-point number. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = '3.5' number = float(text) print(number) | v DISPLAYED RESULT 3.5 FINAL BINDINGS text --> object 1 (str) number --> object 2 (float) object 1: '3.5' object 2: 3.5 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python float() Function – Convert to Floating Point Number. Instructions: text = '3.5' number = float(text) print(number). Displayed result: 3.5. The final example bindings are text --> object 1 (str); number --> object 2 (float). object 1: '3.5'; object 2: 3.5

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = '3.5'
number = float(text)
print(number)
Live state

Top-level scope

text→Object 1
number→Object 2
Object 1 · str'3.5'Object 2 · float3.5
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3.5

What do you expect this program to print?

What does this example display?

text = '3.5'
number = float(text)
print(number)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3.5

Follow the printed values in order. The output is: 3.5

More examples and reference

Python float() Function

The float() function in Python is used to convert a number or a string into a floating-point number (i.e., a decimal number).

This is helpful when you need precision or decimal values in your calculations.

Syntax

float([value])

Parameters:

  • value (optional) – A number or a string that looks like a number.

Returns:

  • A floating-point number (decimal).

Example 1: Convert Integer to Float

x = float(5)
print(x)
5.0

Example 2: Convert String to Float

price = float("19.99")
print(price)
19.99

Example 3: Using float() Without Any Argument

x = float()
print(x)
0.0

If no value is given, float() returns 0.0.

Example 4: Convert Scientific Notation String

num = float("1e3")
print(num)
1000.0

Use Case: Why Use float()?

  • When reading decimal values from user input
  • For precise calculations (e.g., percentages, interest rates)
  • To convert values before arithmetic operations

Common Mistakes

  • Passing a non-numeric string:
  • float("hello")  # ❌ Raises ValueError
  • Expecting exact decimal representations: due to floating-point precision, results like 0.1 + 0.2 may not be exactly 0.3.

Interview Tip

In coding interviews, float() is often used for type conversion before performing arithmetic or formatting results.

Summary

  • float() converts int, str, or no argument to a decimal number
  • Raises ValueError for invalid strings
  • Returns 0.0 if called with no arguments

Practice Problem

Ask the user for a price and discount percentage, then calculate and print the final price.

price = float(input("Enter the price: "))
discount = float(input("Enter the discount %: "))
final_price = price - (price * discount / 100)
print("Final price after discount:", final_price)

What to remember

Connect what you noticed in the example with the idea below.

Remember

float() converts a supported number or numeric string into a floating-point number.

Watch out for this idea

float() accepts every arbitrary word as a numeric value.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →