Convert String to Float Python
python cast string to int.We can convert a string to float in Python using float()
function. It’s a built-in function to convert an object to floating point number.
float()
The float()
method returns a floating point number from a number or a string.
float() Syntax
The syntax for float() is:
float([x])
how to convert string to float in python
Parameters
- It can be an int, float, or string.
- If it is a string, then it must be of correct decimal format.
Returns
- It returns a float object.
- If no argument is provided, then it returns
0.0
. - If the given argument is outside the range of float, it raises overflow Error.
intNum = 44 # convert int to float floatNum = float(intNum) print(floatNum) # Output: 44.0
python convert string to number
strr = '44.8765' flt = float(strr) print(type(flt)) print(flt)
<class 'float'> 44.8765
string to float python
string = '96.9968' flt = float(string) print(type(flt)) print('Value =', flt)
<class 'float'> Value = 96.9968