The float() method is used to return a floating point number from a number or a string.
Syntax:
float(x)
The method only accepts one parameter and that is also optional to use. Let us look at the various types of argument, the method accepts:
- A number : Can be an Integer or a floating point number.
- A String :
- Must contain numbers of any type.
- Any left or right whitespaces or a new line is ignored by the method.
- Mathematical Operators can be used.
- Can contain NaN, Infinity or inf (any cases)
Values that the float() method can return depending upon the argument passed
- If an argument is passed, then the equivalent floating point number is returned.
- If no argument is passed then the method returns 0.0 .
- If any string is passed that is not a decimal point number or does not match to any cases mentioned above then an error will be raised.
- If a number is passed outside the range of Python float then OverflowError is generated.
Now let us look at various examples and the working of float() method.
# Python program to illustrate # Various examples and working of float() # for integers print(float(21.89)) # for floating point numbers print(float(8)) # for integer type strings print(float("23")) # for floating type strings print(float("-16.54")) # for string floats with whitespaces print(float(" -24.45 \n")) # for inf/infinity print(float("InF")) print(float("InFiNiTy")) # for NaN print(float("nan")) print(float("NaN")) # Error is generated at last print(float("Geeks")) |
Output:
21.89 8.0 23.0 -16.54 -24.45 inf inf nan nan
All lines are executed properly but the last one which will return an error:
Traceback (most recent call last):
File "/home/21499f1e9ca207f0052f13d64cb6be31.py", line 25, in
print(float("Geeks"))
ValueError: could not convert string to float: 'Geeks'
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Convert String float to float list in Python
- Python - Convert Float String List to Float Values
- Python - Summation of float string list
- Python | Sort a tuple by its float element
- Python program to convert float decimal to Octal number
- Python | Float type and its methods
- Python | Float Layout in Kivy
- Python | Ways to sort list of float values
- Python | Filter out integers from float numpy array
- Python | Check for float string
- Python | sympy.Float() method
- Python | Min/Max value in float string list
- Python | List of float to string conversion
- Python | Convert tuple to float value
- Python - Filter float strings from String list
- Python - Convert Float to digit list
- Python | Average of Float Numbers
- Python | Convert Joint Float string to Numbers
- Convert String to Float in Python
- Python - Produce K evenly spaced float values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

