Convert String to Float in Python(6 ways)

In this post, we will learn, how to Convert String to Float in Python or convert a list of string values to a list of floats with examples. Whenever we have string float values, performing any mathematical operation on it results in an error or invalid output. Let us suppose we have a = ‘18.90’ and b = ‘12.34’ contain string float values, adding these numbers results in invalid output because instead of adding it will concatenate these two variables’ values.

1. Convert String to Float in Python using float()


The python built-in float() function returns a floating-point number converted from any data type to float. It takes one argument and converts it into float. If no parameter is provided it returns 0.0. We can convert both positive and negative numbers to float. In case if string representation of float value contains invalid float value then ValueError: could not convert string to float: ’10f.123′ is raised.

num = '369.362'
float_num = float(num)
print(type(num))
print('str to float =', float_num)

negnum = '-234.569'
print('neg str to float :',float(negnum))


num2 = '36,9.362'
print(float(num2 ))

Output

<class 'str'>
str to float = 369.362
neg str to float : -234.569
ValueError: could not convert string to float: '36,9.362'

2. Convert string to float with a comma and dot


In the above example converting a string float value with a comma to a float value results in a value error. To fix this error and convert a string float value with a comma, the string replace() method is used that replaces the comma with a blank, and the float() function will convert the string to float.

num2 = '36,9.362'

print('original type:',type(num2))

float_num = float(num2.replace(',',''))

print('str to float =', float_num)

print('type after conversion:',type(float_num))

Output

<class 'str'>
str to float = 369.362
<class 'float'>

3. Convert string to float with 2 decimal places


Sometimes we have to convert the string float values to float values with some decimal place. The curly brackets { } are used as a placeholder where the value of the variable will be added. We use the precision placeholder like {:,2f} for 2 decimal places,{:,3f} for 3 decimal places so on as per need, followed by the string format() method.

num = '369.362'

print(type(num))

float_num = float(num)

Two_dec_plac = "{:.2f}".format(float_num)

print('str to float =', Two_dec_plac)
print(type(float_num))


Output

<class 'str'>
str to float = 369.36
<class 'float'>

4. Convert a list of strings to float using list comprehension


In this example, we have a list of string float values to list of float my using float() and list comprehension. We are iterating through each element of the list using the list comprehension and on each iteration applying, float() function on the element after the iteration is over returns the list string to the list of float.

numlst = ['369.362','39.60','49.50','56.10']

print(type(numlst))


reslst = [float(item) for item in numlst]

print('str to float =', reslst)

Output

<class 'list'>
str to float = [369.362, 39.6, 49.5, 56.1]

5. Convert a list of strings to float Using NumPy


We have used the numpy library to convert a list of strings to float. To use this library make sure it is installed on the local machine. In case of already installed numpy library can be imported in code using “import numpy as np” and use it astype() method to convert string to float.

import numpy as np

npArr = np.array(['369.362','39.60','49.50','56.10'])

reslst = npArr.astype(float)

print('str to float =', reslst)


Output

str to float = [369.362  39.6    49.5    56.1  ]

6. Convert a list of strings to float using a map()


The map() function applies a function on each element of iterable (list, tuple) and returns a map object. In this below example we are iterating over the list and applying the float function on each element of iterable(list) and finally converting the return map object to a list with the help of the list() function.

numLst = ['369.362','39.60','49.50','56.10']

reslst = list(map(float, numLst))

print('str to float =', reslst)

Output

str to float = [369.362, 39.6, 49.5, 56.1]

Summary

In this post, we have learned how to Convert String to Float in Python with examples by using the python function float. We have covered Convert String to Float in Python, Convert a list of strings to a list of float