Convert String to Float in Python

Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting the string to float. In Python, we can use float() to convert String to float.

Float()

This function is used to convert any data type to a floating-point number.

Syntax:

float(x)

The method only accepts one parameter and that is also optional to use. If no argument is passed then the method returns 0.0.

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

string= "3.141"
  
print(string)
print(type(string))
  
# converting string to float
Float = float(string)  
  
print(Float)
print(type(Float))

chevron_right


Output:

3.141
<type 'str'>
3.141
<type 'float'>

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

string = '55.567'
  
float = float(string)
  
print(type(float))
print('Float Value =', float)

chevron_right


Output:

<type 'float'>
('Float Value =', 55.567)
My Personal Notes arrow_drop_up


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.