- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Float method is part of python standard library which converts a number or a string containing numbers to a float data type. There are following rules when a string is considered to be valid for converting it to a float.

The string must have only numbers in it.

Mathematical operators between the numbers can also be used.

The string can represent NaN or inf

The white spaces at the beginning and end are always ignored.

The below program indicates how different values are returned when float function is applied.

n = 89 print(type(n)) f = float(n) print(type(f)) print("input",7," with float function becomes ",float(7)) print("input",-21.6," with float function becomes ",float(-21.6)) print("input NaN, with float function becomes ",float("NaN")) print("input InF, with float function becomes ",float("InF"))

Running the above code gives us the following result −

<class 'int'> <class 'float'> input 7 with float function becomes 7.0 input -21.6 with float function becomes -21.6 input NaN, with float function becomes nan input InF, with float function becomes inf

Passing a stream without having any numeric values throws error.

print("input Tutorials, with float function becomes ",float("Tutorials"))

Running the above code gives us the following result −

Traceback (most recent call last): File "C:/xxx.py", line 18, in print("input Tutorials, with float function becomes ",float("Tutorials")) ValueError: could not convert string to float: 'Tutorials'

- Related Questions & Answers
- Python Float Layout in Kivy?
- Convert tuple to float value in Python
- How to convert float to integer in Python?
- What is the maximum value of float in Python?
- Python - Filter out integers from float numpy array
- How to convert Float array list to float array in Java?
- How do you round up a float number in Python?
- Python program to convert float decimal to octal number
- Float and Double in C
- Comparison of Float in Java
- Comparing float value in PHP
- How elements float in HTML?
- How to parse a string to float or int in python?
- How to display a float with two decimal places in Python?
- Java Float isNaN() Method

Advertisements