Convert String to Float Convert String to Float

Python Convert String to Float

In Python, strings are byte sequences that represent Unicode characters. Due to the lack of a character data type in Python, a single character is just a one-length string. To access the string’s components, use square brackets. To generate strings in Python, single quotes, double quotes, and even triple quotes can be used.

This article will go over various methods to convert string to float.

Example:

Input:

'34.124'

Output:

34.124

String to Float Conversion

We’ll look at how to convert a number string to a float object .

Form conversion functions in Python are used to transform one data type to another. The aim of this article is to provide details on how to convert a string to a float. To convert a String to a float in Python, use the float() function.

Float()

Any data form can be converted to a floating-point number using this function.

Syntax:

float(object)

Only one parameter is accepted by the process, and it is also optional to use. The method returns 0.0 if no argument is transmitted.

Parameters:

  • An int, float, or string may be used.
  • If it’s a string, it has to be in the right decimal format.

Return:

  • It gives you a float object as a result.
  • ValueError will be raised if the given string contains something other than a floating-point representation of a number.
  • If no argument is given, 0.0 is returned.
  • Overflow Error is raised if the given statement is beyond the range of float.

Let’s look at some examples of how to convert a string to a float object using the float() function.

Assume we have a Str object with the string ‘34.124′. We’ll transfer the string to the float() function to convert it to a floating-point number, or float entity. Which returns the float object after converting this string to a float. As an example,

# Driver code

# given string value
stringval = '34.124'

# converting string to float
floatvalue = float(stringval)

# printing the float number
print(floatvalue)

# printing the type of the number
print('Type of the number/object :', type(floatvalue))

Output:

34.124
Type of the number/object : <class 'float'>

Convert a comma-separated number string to a float object

Let’s say we have a string called ‘34,123.454’  which includes the number but also some commas. It’s a little difficult to convert this type of string to float. If we transfer this to the float() function directly, we’ll get an error as below:

ValueError: could not convert string to float: '34,123.454'

float() returned an error since string contained characters other than digits. So, before passing the string to the float() function, we need to delete all the extra commas.

We use replace function to replace commas with blank character such that commas  are deleted

Below is the implementation:

# Driver code

# given string value
stringval = '34,123.454'

# convert string with comma to float
# replacing ,(comma) with blank value
floatvalue = float(stringval.replace(',', ''))

# printing the float number
print(floatvalue)

# printing the type of the number
print('Type of the number/object :', type(floatvalue))

Output:

34123.454
Type of the number/object : <class 'float'>

Related Programs: