Python Convert String to Float

Introduction

In this article, we will use Python’s float() function to convert strings to floats. And we will also use Python’s str() function to convert floats to strings.


It is important to properly convert the data types before using them for calculations and concatenations in order to prevent runtime errors.


Using the float() function


We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.


Example


Let’s look at an example of converting a string to float in Python:


input_1 = '10.5674' input_1 = float(input_1)print(type(input_1))
print('Float Value =', input_1)

Output:



Float Value = 10.5674

The string value of '10.5674' has been converted to a float value of 10.5674.


Why do we need to convert a string to float?


If we are receiving float value from user input through the terminal or reading it from a file, then they are string objects. We have to explicitly convert them to float so that we can perform necessary operations on them, such as addition, multiplication, etc.


input_1 = input('Please enter first floating point value:
'
)
input_1 = float(input_1) input_2 = input('Please enter second floating point value:
'
)
input_2 = float(input_2)print(f'Sum of {input_1} and {input_2} is {input_1+input_2}')


Note: If you are not familiar with string formatting using f prefix, please read f-strings in Python.



Let’s run this code and provide float values for input_1 and input_2:


Please enter first floating point value:
10.234
Please enter second floating point value:
2.456
Sum of 10.234 and 2.456 is 12.69

The resulting sum of 10.234 and 2.456 is 12.69.


Ideally, we should use a try-except block to catch exceptions in case of invalid input from the user.


Using the str() function


We can also convert a float to a string using the str() function. This might be required in situations where we want to concatenate float values.


Example


Let’s look at an example:


input_1 = 10.23
input_2 = 20.34
input_3 = 30.45 # using f-string from Python 3.6+, change to format() for older versions
print(f'Concatenation of {input_1} and {input_2} is {str(input_1)+ str(input_2)}')
print(f'CSV from {input_1}, {input_2} and {input_3}:
{str(input_1)},{str(input_2)},{str(input_3)}'
)
print(f'CSV from {input_1}, {input_2} and {input_3}:
{", ".join([str(input_1),str(input_2),str(input_3)])}'
)

Let’s run this code:


Concatenation of 10.23 and 20.34 is 10.2320.34
CSV from 10.23, 20.34 and 30.45:
10.23,20.34,30.45
CSV from 10.23, 20.34 and 30.45:
10.23, 20.34, 30.45

Concatenating 10.23 and 20.34 produces the string '10.2320.34'. This code also produces two versions of comma-separated values (CSV).


If we don’t convert float to string in the above program, the join() function will throw an exception. Also, we won’t be able to use the + operator to concatenate as it will add the floating point numbers.


Conclusion


You can check out the complete Python script and more Python examples from our GitHub repository.


References: