Python Convert String to Float

Convert string to float and float to string in python; Through this tutorial, i am going to show you how to convert string to float and convert float to string in python.

How to convert string to float and float to string in python

  • Python Convert String to float
  • Python Convert float to String

Python Convert String to float

Use the python float() method to convert string to float; see the following example:

s = '10.5674'
f = float(s)
print(type(f))
print('Float Value =', f)

Output:

<class 'float'>
Float Value = 10.5674

Python program to convert a string to float:

See the python program to convert a string to float; as shown below:

num = "3.1415"
print(num)
print(type(num))  # str
pi = float(num)  # convert str to float
print(pi)
print(type(pi))  # float

Output

3.1415
<class 'str'>
3.1415
<class 'float'>

Python Convert float to String

Use the python str method to convert float to string.

python program converts float to string:

pi = 3.1415
print(type(pi))  # float
piInString = str(pi)  # float -> str
print(type(piInString))  # str

Output:

<class 'float'>
<class 'str'>

Recommended Python Tutorials

Leave a Comment