Python – Check for float String


The strings consist of characters and to transform them into float data type, Python facilitates us with various methods using the inbuilt function. With the help of these inbuilt functions, we can check for the float strings easily. In this article, the user will learn how to check whether a string contains a float value. To achieve this, three strategies are demonstrated to verify float string. Isdigit() method is also used to check if the given string has only digits. Then the count() and replace() are used to check if the string contains decimal values and other characters of the numbers.

Approaches

Approach 1 − Using try-except block

Approach 2 − Using Regular Expression

Approach 3 − Using isdigit() method

Approach 1: Python Program to check for float string using try-except block

The function is defined with a string variable and using the try-except method, the print statement returns the value as “True” or “False”.

Algorithm

  • Step 1 − The function is defined with an argument that holds the “str” variable.

  • Step 2 − If the argument contains the float value then it returns “True” otherwise, it will return “false”.

  • Step 3 − Inside the function, the float() method is used to convert the given string into a float type, and upon succession, it returns true or else it will return false.

  • Step 4 − The input “str1” is initialized with the float string value.

  • Step 5 − Depending on the value of str, it prints the statement.

Example

#function is defined with one parameter as str
def str_float(str1):
   try:
      int(str1)
      #returns false if it is an integer value
      return False
   except ValueError:
      #returns error if it is not a float value
      pass
   try:
      #float method is used to convert the given string to float
      float(str1)
      #returns True if it is a float value
      return True
   except ValueError:
      #returns false if it is not a float value
      return False
#str is initialized with a float value
str1 = "38.90"
if str_float(str1):
   #if true this statement will be returned
   print("Yes! float string")
else:
   #if false this statement will be returned
   print("No! float string ")

Output

Yes! float string

Approach 2: Python Program to check for float string using regular Expression

The function defines the regular expression pattern which includes all the possibilities that could be inside the given input string like the signs( + or -), integers ( from 0 to many), exponent, and decimal values.

Algorithm

  • Step 1 − The “re” library is imported to use the regular expression method.

  • Step 2 − The function is defined with an argument that holds the “str” variable.

  • Step 3 − When the string is of float data type it returns “True” otherwise, it will return “false”.

  • Step 4 − Inside the function, the re.match() method is used to find the match between the input string along with the given regular expression.

  • Step 5 − Initialize the stg variable containing float value.

  • Step 6 − Depending on the value of str, it prints the statement.

Example

#importing the re module
import re

#function is defined with one parameter as stg
def str_float(stg):
   regular_exp = re.compile("r'^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$")
   if regular_exp.match(stg):
      if "." in string:
         return True
      else:
         return False
   else:
	   return False#str is initialized with a float value
stg = "38.90"
if str_float(stg):
   #if true this statement will be returned
   print("Yes! float string")
else:
   #if false this statement will be returned
   print("No! float string")

Output

Yes! float string

Approach 3: Python Program to check for float string using isdigit() method

Algorithm

  • Step 1 − The function is defined with an argument that holds the “str” variable.

  • Step 2 − The Boolean function is used to return the values upon checking the string with three functions namely isdigit(), count(), and replace() method.

  • Step 3 − When the string is of float data type it returns “True” in the first two conditions otherwise it will return “false”.

  • Step 4 − The input “str1” is specified with the float string value.

  • Step 5 − Finally, prints the statement.

Example

# function is defined with one parameter as str
def str_float(str1):
   if str1.isdigit():
      return False
   else:
      try:
         float(str1)
         return True
      except ValueError:
         return False# str is initialized with a float value
str1 = "38.90"
if str_float(str1):
   # if true this statement will be returned
   print("Yes! float string")
else:
   # if false this statement will be returned
   print("No! float string")

Output

Yes! float string

Conclusion

The Python program is exclusively used to minimize manual work and error. To simply check whether the given input of a string is a float or not, there are many methods. Some are discussed above using the try and except method and using isdigit() functions to check for float string and also it returns false when the input is an integer.

Updated on: 25-Aug-2023

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements