How to Convert String to Float in Python?
Overview
When working with terminal or file operations in Python programming, such as reading or writing a file, the user input is a string object. Although you requested a floating-point value, Python's input function returns the user input as a string object. As a result, you must explicitly convert the string to a floating-point value in order to do the required operations. Changing data types in Python is a common practice, and Python includes functions to make this easier.
Convert String to Float Value in Python
In Python, the default values that are fed by the user as the input are in string format, and many times we have to deal with the floating point, so we would have to explicitly convert the string values to float values. This can be done using several ways, such as by using the float function.
The conversion to float value can also be done using the NumPy, and the conversion can also be some specified format such as fixed decimal point conversion or converting the strings with a comma to floating-point values. Here we would see how we can convert the list of strings to float values. Several different methods which we would be learning several ways along with different examples to convert the string to float such as:-
- Using float() function
- Convert string having comma to a floating-point value
- Convert the list of strings to float
- Conversion to float value using the Numpy
- Conversion to specified decimal point from string
Using float() Function
float() function is the most common way to convert the string to the float value. The float function takes the parameter y, which is a string, and would then convert the string to a float value and return its float value. While using the float function, we should keep in mind that the parameter y should have only the decimal value or the integer values, which can be converted to float value by the float function.
The value 0.0 is returned in the case when there is no argument provided to the function. Overflow error is given when the argument that is given as input cannot be contained by the floating-point data type.
Example:
Output:
Convert a Number String with Commas into a Floating-point Object
If we have the string which contains a comma such as 1,2.56, we can not directly convert the string to float using the float() function as there the condition was that the parameter should only be in integer or decimal form, so we would have to firstly remove the comma and then use the flat function to convert the string value which contains the comma or decimal point to float value.
For removing the comma from the string, we would be replacing the comma with "" which would remove the comma from the string.
Example:
Output:
Convert String to Float List in Python
If the string contains multiple float values, then we would have to, one by one, iterate over the value s in the string separated by a comma and then convert them to float values.
For example the string value be s=34.25, 36.24, 15.26, 47.51, 97.51 the the float list would contain the values [34.25, 36.24, 15.26, 47.51, 97.51]
Example:
Output:
Convert a List of Strings to Float in Python
For converting the list of strings to a floating value list, we would have to iterate the string list and take the values one by one and then convert the string values to the floating values and then append all the float values to the floating value list.
For example the string list contains the value s=['34.25', '36.24', '15.26', '15.48', '15.30'] and the resulting float values would be [34.25, 36.24, 15.26, 15.48, 15.30]
Example:
Output:
Convert String to Float Using Numpy
Numpy is a python library that allows users to execute high-level mathematical operations on massive, multi-dimensional arrays and matrices. The first step in using NumPy to convert a string to a float is to import the NumPy library, which would be used to convert the string to the float values. Later, you can convert the string to a floating-point value in Python using the astype method, which is an in-built technique.
Example:
Output:
Convert String to Float with Specified Decimal Points
For converting the string to specified string to some specified decimal points, we would first be converting the values to the float values and then converting the float values to some specified decimal values. We would be using the format() function, which has the syntax {:.Nf}.format(float value) where Nf is the number of values that must be present after the decimal in the floating value.
Example:
Output:
Conclusion
- Many times while writing python programs, we have to deal with the float values, and generally, when we take the input from the user, the default datatype is a string, and then we have to convert it to float type.
- For converting the string to float type, we have discussed several methods above, such as:*
- String to float
- List of strings to float
- String with a comma to float
- Float having specified decimal format
For all above types of conversion, we have used the simple basic approach:
- Convert the string values into the form of an integer and a decimal.
- In case the string is a list, we would have to iterate over the string and deal with each value separately.
- For converting string value to float, we have used the float() function to convert the string value to the float value.
- For converting float values to the specified decimal format, we can use the format() function.