How To Check If A String Can Be Converted To A Float In Python?

Check if a string can be converted to a float in Python

To check if a string can be converted to a float in Python, you can use the float() function or isdigit() function. Follow the article to better understand.

Check if a string can be converted to a float in Python

The float number is a data type in Python. Float in Python is a number represented as a floating point float number. You can understand that the python float or float number data type is numbers containing decimals after the dot.

Example:

10.1

9.00

The string is a common data type in Python. A word or a paragraph is also called a type. A string in Python begins with a single or double quote. Whichever sign it begins, it must end with that.

Example: “hello. my name is john” is a string.

To check if a string can be converted to a float in Python, I have the following ways:

Use float() function

The float() function converts the specified value to a floating point number.

Syntax:

float([x])

The function can accept the following value types as arguments:

  • Number: integer or decimal.
  • String: Contains any number type. The program ignores the left or right space or a new value line. Mathematical operators can be used. It can be used with NaN, Infinity or inf.

The return value depends on the parameter passed:

  • If the parameter passes as required, the function returns a floating number.
  • No parameter passed returns 0.0
  • The program will report an error if the input parameter is not a decimal number or the program does not meet the requirements.
  • Parameters out of float range will report ‘exception OverflowError.’

Example:

  • Initialize two strings
  • Using float(). Check for float string
  • If it is a float, return True. Otherwise, return False.
str1 = "Learnshareit1"
str2 = "18.18"

def isConvertToFloat(strParam):
    try:
        float(strParam)
        return True
    except ValueError:
        return False

print(f"{str1} can be converted to a float: ", isConvertToFloat(str1))
print(f"{str2} can be converted to a float: ", isConvertToFloat(str2))

Output:

Learnshareit1 can be converted to a float:  False
18.18 can be converted to a float:  True

Use the isdigit() function 

I will refer you to the method isdigit() function to check if a string is a float or not. I will combine the isdigit() function with the replace() function.

Syntax:

str.isdigit()

Parameters:

  • No parameters.

Example:

  • Initialize two strings.
  • Using isdigit() function combined with replace() function to check for float string.
  • The replace() function will replace the ‘.’ appears in two strings (if any) the purpose is to check if the string contains a number or not.
  • If a string has only digits, return True; otherwise, return False.

The isdigit() function returns True if the string contains only digits and False otherwise.

str1 = 'Learnshareit1'
str2 = '18881.13'

# Using isdigit() function combined with replace() function to check for float string
print(f"{str1} can be converted to a float: ", str1.replace('.', '', 1).isdigit())
print(f"{str2} can be converted to a float: ", str2.replace('.', '', 1).isdigit())

Output:

Learnshareit1 can be converted to a float:  False
18881.13 can be converted to a float:  True

Summary

Please leave a comment below if you don’t know how to check if a string can be converted to a float in Python. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *