Parameter
The float() function takes one optional parameter:
x (optional): The number or string to be converted to a floating-point number. It can be an integer, a floating-point number, or a string that represents a number. If no argument is passed, the function returns 0.0.
Here are a few more examples to illustrate the parameter:
Python
# convert an integer to a float
num1 = float(5)
print(num1) # Output: 5.0
# convert a floating-point number to a float
num2 = float(3.14)
print(num2) # Output: 3.14
# convert a string to a float
num3 = float("2.5")
print(num3) # Output: 2.5
# convert a string with an integer value to a float
num4 = float("7")
print(num4)

You can also try this code with Online Python Compiler
Run Code
Output
5.0
3.14
2.5
7.0
In the above examples, we pass different types of arguments to the float() function. It can handle integers, floating-point numbers, and strings that represent numbers. The function converts them to floating-point numbers accordingly.
Return values
The float() function returns a floating-point number that is equivalent to the number or string passed as the argument. Here are the possible return values:
- If a number (integer or floating-point) is passed as the argument, the function returns the equivalent floating-point number.
- If a string that represents a floating-point number is passed as the argument, the function returns the equivalent floating-point number.
- If a string that represents an integer is passed as the argument, the function returns the equivalent floating-point number.
- If no argument is passed or the argument is an empty string, the function returns 0.0.
Examples
Python
# return value when passing a number
result1 = float(10)
print(result1)
# return value when passing a string representing a floating-point number
result2 = float("3.14")
print(result2)
# return value when passing a string representing an integer
result3 = float("5")
print(result3)
# return value when passing no argument or an empty string
result4 = float()
print(result4)
result5 = float("")
print(results)

You can also try this code with Online Python Compiler
Run Code
Output
10.0
3.14
5.0
0.0
0.0
In the above examples, we see that the float() function returns the equivalent floating-point number based on the argument passed. When no argument or an empty string is passed, it returns 0.0.
float() in Python Example
Now let's look at some practical examples of using the float() function in Python:
Example 1: Converting an integer to a float
Python
num = 10
float_num = float(num)
print(float_num)

You can also try this code with Online Python Compiler
Run Code
Output
10.0
Example 2: Converting a string to a float
Python
string_num = "3.14"
float_num = float(string_num)
print(float_num)

You can also try this code with Online Python Compiler
Run Code
Output
3.14
Example 3: Converting a string with an integer value to a float
Python
string_num = "7"
float_num = float(string_num)
print(float_num)

You can also try this code with Online Python Compiler
Run Code
Output
7.0
Example 4: Performing arithmetic operations with floats
Python
num1 = float("2.5")
num2 = float("1.5")
sum_result = num1 + num2
print(sum_result)
product_result = num1 * num2
print(product_result)

You can also try this code with Online Python Compiler
Run Code
Output
4.0
3.75
Example 5: Using float() in user input
Python
user_input = input("Enter a number: ")
float_num = float(user_input)
print("The entered number is:", float_num)

You can also try this code with Online Python Compiler
Run Code
Output
Enter a number: 3.14
The entered number is: 3.14
In these examples, we see how the float() function is used to convert integers and strings to floating-point numbers. We can perform arithmetic operations with floats, such as addition and multiplication. The float() function is also commonly used when accepting user input to convert the input string to a float for further processing.
Python float() Exceptions and Errors
When using the float() function in Python, there are a few common exceptions and errors that you may encounter:
ValueError
If you pass a string that does not represent a valid floating-point number, the float() function will raise a ValueError. This occurs when the string contains non-numeric characters or an invalid format.
Example:
Python
num = float("abc")

You can also try this code with Online Python Compiler
Run Code
Output
ValueError: could not convert string to float: 'abc'
TypeError
If you pass an argument of an unsupported data type to the float() function, it will raise a TypeError. This happens when the argument is not a number or a string.
Example:
Python
num = float([1, 2, 3])

You can also try this code with Online Python Compiler
Run Code
Output
TypeError: float() argument must be a string or a number, not 'list'
OverflowError
If the number passed to the float() function is too large or too small to be represented as a floating-point number, an OverflowError will be raised.
Example-1
Python
num = float("1e1000")

You can also try this code with Online Python Compiler
Run Code
Output
OverflowError: float() argument is too large
To handle these exceptions, you can use a try-except block to catch and handle the specific exception gracefully.
Example-2
Python
try:
num = float("abc")
print(num)
except ValueError as e:
print("Invalid input:", str(e))

You can also try this code with Online Python Compiler
Run Code
Output
Invalid input: could not convert string to float: 'abc'
Frequently Asked Questions
Can the float() function convert a string with leading or trailing whitespace to a float?
Yes, the float() function automatically strips leading and trailing whitespace from the string before converting it to a float.
What happens if you pass an empty string or no argument to the float() function?
If you pass an empty string or no argument to the float() function, it returns 0.0.
Can you convert a float back to an integer using the float() function?
No, the float() function is used to convert a number or a string to a floating-point number. To convert a float to an integer, you can use the int() function.
Conclusion
In this article, we learned about the float() function in Python, which is used to convert a number or a string to a floating-point number. We also looked at common exceptions and errors that can occur when using the float() function and how to handle them.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.