Python float Function | Convert String and Number

The float() function returns a floating-point number from a number (integer) or a string. It’s a basic inbuilt function in python, you don’t need to import any module.

Float() function used for converts the specified value into a floating-point number.

Syntax

Return value from float() function

  • Equivalent floating-point number if an argument is passed
  • 0.0 if no arguments passed
  • OverflowError exception if the argument is outside the range of Python float

Example: Python float function

1. Convert a string into a floating-point number:

Output:

Python float Function

2. Convert a Number into a floating-point number:

More Examples:-

Look at the below code program of various examples and the working of the float() method.

Output:

21.89
8.0
47.0
-77.56
-34.75
inf
inf
nan
nan

Last line Error: could not convert string to float

could not convert string to float

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2019.3 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.