float()

float() returns floating point number by taking string or integer as input .

Here are some examples with different type of input numbers
print(float())       # 0.0 
print(float(5))      # 5.0
print(float("34.5")) # 34.5

String inputs

print(float("  45.8 ")) # 45.8
print(float("InF"))     # inf
print(float("NaN"))     #nan
print(float("Infinity"))#inf
We will get error message based on input
print(float('abc45'))
Output
ValueError: could not convert string to float: 'abc45'

Data type

We can check type of data by using type(). Here we have used one float number.
print(type(4.5)) # <class 'float'>

Handling Conversion Errors

Use try-except to handle cases where the input is not a valid float:

try:
    print(float('abc'))
except ValueError:
    print("Invalid input for float conversion")

Use Case: Parsing User Input

In web forms or data input systems, user input is often received as a string. You can use float() to safely convert this data into a usable floating-point number:

user_input = "45.67"
converted_value = float(user_input)
print(converted_value)  # Output: 45.67

Special Values

The float() function can handle special values like inf and NaN:

print(float('NaN'))   # Output: nan
print(float('Inf'))   # Output: inf

bin() int() complex()


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer