Friday, November 15, 2024
Google search engine
HomeLanguagesConvert String to Float in Python

Convert String to Float in Python

Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting the string to float. In Python, we can use float() to convert String to float. and we can use int() to convert a String to an integer.

Input: "33.28"
Output: 33.28 # float
Explanation: int in input and float in output 

Converting String to Float

Below are the lists of methods that we will cover in this article:

  • Parse a String to a Float using float()
  • Convert String to Float with Specified Decimal Points
  • Convert String to Float Using Numpy
  • FAQ

Converting String to Float

In Python, we can convert String datatype to float by using a built-in method float which converts the data type of the string passed in it.

Python3




string = "33.28"
  
# using float method to convert string to float
num = float(string)
  
print(num)
print(type(num))


Output:

33.28
<class 'float'>

Python Program to Parse a String to a Float using float()

This function is used to convert any data type to a floating-point number. This method only accepts one parameter and that is also optional to use. If no argument is passed then the method returns 0.

Python3




string= "3.141"
  
print(string)
print(type(string))
  
# syntax - float()
Float = float(string)  
  
print(Float)
print(type(Float))


Output:

3.141
<type 'str'>
3.141
<type 'float'>

Time complexity: O(1), as there is a fixed number of operations being performed regardless of the size of the input.
Auxiliary space: O(1), as the amount of memory used by the code does not depend on the size of the input.

ValueError Exception 

In this example, we try to convert a string into an integer and then a float. In the output we can see that we can’t convert a string float number into an integer thus, we get a value error.

Python3




string = '55.567'
  
Float = float(int(string))
  
print(type(Float))
  
print('Float Value =', Float)


Output:

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))
ValueError: invalid literal for int() with base 10: '55.567'

Convert String to Float with Specified Decimal Points

To convert a string to a float with a specified number of decimal points, you can use the float() function in Python along with string formatting.

Python3




string_number = "55.5675751"
decimal_points = 2
  
#converting string to float
float_number = float(string_number)
#using .format method to print upto decimal point
formatted_float = "{:.{}f}".format(float_number, decimal_points)
  
print(formatted_float)


Output:

55.57

Convert String to Float Using Numpy

In Numpy we have a ‘numpy.float64‘ which is an inbuilt method in the Numpy module that converts the string value to float.

Here is an example of it.

Python3




import numpy as np
  
my_str = "55.567"
float_number = np.float64(my_str)
print(float_number)
print(type(float_number))


Output:

55.567
<class 'str'>

FAQs on float and string-type conversation

Q: Why do need to convert float to string?

Answer: Sometimes we need to convert float value into string value because we want to use the method which is there in Python only for string type.e.g., string concatenation.

Q: Why do we need to convert a string into a float?

Answer: As per requirement if we want to use the method which is available for the float datatype then we will convert the string into float value e.g., addition.

RELATED ARTICLES

Most Popular

Recent Comments