float() in Python | Python float() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python Programming, float() is an inbuilt function that converts an integer or specific strings into floating-point values.

Syntax of float() function in Python

Parameters of float() function in Python

It accepts only one parameter but it's optional:

  • x (Optional) - number or string that needs to be converted to floating point number If it's a string, the string should contain decimal points.

Return values of float() function in Python

Argument typeInputReturn valueOutput
No argument is passed-0.00.0
Integer4Floating point number4.0
Floating point number30.0Floating point number30.0
String"20"Floating point number20.0
String"nan", "NaN"nannan
String"inf", "InF", "InFiNiTy", "infinity"infinf
String"Scaler"ValueError: could not convert string to float : 'Scaler'-

Example of float() function in Python

Let's look at an example to convert string 30 to floating-point value 30.0:

Code:

Output

What is float() function in Python?

In all programming languages, typecasting of the data has been a important feature.

Python provides us an inbuilt function to convert integers and few strings to floating point numbers. Let's see how float in Python works with different parameters:

  • If no argument is passed then 0.0 is returned.
  • If any number is passed, then floating point number is returned.
  • If a string is passed which is not a decimal point number or strings mentioned above, then ValueError is raised.
  • If a number passed is outside the range of Python float() then OverflowError is generated.

More Examples

Example 1: How float() works in Python?

Python code to explain the working of the float() in Python.

Code:

Output:

Explanation:

The last string passed does not have a decimal point number hence the code returns ValueError.

Example 2: Converting a string into a floating-point number

Code:

Output:

Explanation:

The string passed into the float() in the Python program contains integers and hence the program returns the required floating-point value.

Example 3: Using float() with infinity and Nan(Not a number)

Code:

Output:

Only for the specified strings, the float() in Python returns the above output.

Example 4: Converting an Integer to a Float in Python

Code:

Output:

Explanation:

The above code represents when an integer is passed into the float function. The code will always return the required floating-point value in such cases.

Example 5: Python float() Exception

Code:

Output:

Explanation:

In the above code, a string is passed to the float() function to check if it is a decimal point value. But it turns out not to be, hence the code returns an error.

Conclusion

  • float() in Python is an inbuilt function that is used to convert any number or specific strings to a floating-point number.These specific strings are: "nan", "NaN", "inf", "InF", "InFiNiTy", "infinity".
  • If other strings are given as input then the code returns an error i.e., ValueError.