Table of contents
1.
Introduction
2.
Python float() Function Syntax
2.1.
Python
3.
Parameter
3.1.
Python
4.
Return values
5.
Examples
5.1.
Python
6.
float() in Python Example
6.1.
Example 1: Converting an integer to a float
6.2.
Python
6.3.
Example 2: Converting a string to a float
6.4.
Python
6.5.
Example 3: Converting a string with an integer value to a float
6.6.
Python
6.7.
Example 4: Performing arithmetic operations with floats
6.8.
Python
6.9.
Example 5: Using float() in user input
6.10.
Python
7.
Python float() Exceptions and Errors
7.1.
ValueError
7.2.
Python
7.3.
TypeError
7.4.
Python
7.5.
OverflowError
7.6.
Python
7.7.
Python
8.
Frequently Asked Questions
8.1.
Can the float() function convert a string with leading or trailing whitespace to a float?
8.2.
What happens if you pass an empty string or no argument to the float() function?
8.3.
Can you convert a float back to an integer using the float() function?
9.
Conclusion
Last Updated: Jun 30, 2024
Easy

Float() in Python

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The float() function in Python is used to convert a number or a string to a floating-point number. It is a built-in function that returns a floating-point number from a number or a string. 

Float() in Python

In this article, we will learn about the float() function in Python, its syntax, parameters, return values, and examples. We will also look at some common exceptions and errors that can occur when using the float() function.

Python float() Function Syntax

float([x])


Here, x is an optional parameter. It can be a number or a string that represents a floating-point number. If no argument is passed, the function returns 0.0.

For example:

  • Python

Python

# convert an integer to a float

num1 = float(10)

print(num1)  # Output: 10.0

# convert a string to a float

num2 = float("3.14")

print(num2)  # Output:

# call float() without any argument

num3 = float()

print(num3)
You can also try this code with Online Python Compiler
Run Code


Output

10.0
3.14
0.0


In the above examples, we pass an integer 10, a string "3.14", and no argument to the float() function. The function converts the integer and string to floating-point numbers and returns 10.0 and 3.14, respectively. When no argument is passed, it returns 0.0.

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

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:

  1. If a number (integer or floating-point) is passed as the argument, the function returns the equivalent floating-point number.
     
  2. If a string that represents a floating-point number is passed as the argument, the function returns the equivalent floating-point number.
     
  3. If a string that represents an integer is passed as the argument, the function returns the equivalent floating-point number.
     
  4. If no argument is passed or the argument is an empty string, the function returns 0.0.

Examples

  • Python

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

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

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

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

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

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

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

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

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

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass