In this post, we are going to learn how to Check numbers float in Python by using some built-in functions. We will ask the user to input a number and check the type of input based on whether it returns True or False. isinstance() and regular expression.
How to Check number is float Python
- type() :use type() function to check input number is float
- isinstance(): use to check if the number is float
- Regular Expression: to check float input is a floating-point number.
- Regular Expression: check if a string is a floating-point number in python using Regular Expression
1. Check number is float using type() function
In this Python program example, We will learn how to check if the number is float python using a built-in type() function to check if the input number is floating. The type function will return the type of input object. If it returns float that means input is float type Else it is not a float number. Let us understand with the below program.
input_num = eval(input("Please Enter input a number :"))
if type(input_num) ==float:
print("Number is Float",type(input_num))
else :
print('input number is not float')
Output
Please Enter input a number :9.3
Number is Float <class 'float'>
Please Enter input a number :14
input number is not float
2. How to Check number is float python using isinstance()
Python inbuilt isinstance() function checks if a number has type float. The isinstance() function returns True if given an object of the specified type is float. In this example, we are checking if the input_num object is of a specified type float. using isinstance() function.
input_num = eval(input("Please Enter input :"))
if isinstance(input_num,float):
print("Number is float:",isinstance(input_num,float))
else:
print("Number is float:",isinstance(input_num,float))
Output
Please Enter input :15.6
Number is float: True
Please Enter input :15
Number is float: False
3. Regex Expression to check input number is float
In this python program example, we have used the regular expression to check the input number is float use regular expression in python first we have to import the module “Re” and define a regular expression that validates the input number as float by using re.search() method.
- We are asking the user to input a number.
- the re module search() method takes input number and Regular Expression to check input number is a float.
- It returns true if the input is float and False if not a float number.
#How to Check number is float Python
import re
input_num = input("Please Enter input :")
regex = '[-+]?\d*\.\d+|\d+"'
def check_num(input_num):
if(re.search(regex, input_num)):
print("Input is Floating point number")
else:
print("Input is not a Floating point number")
check_num(input_num)
Output
Please Enter input :45.9
Input is Floating point number
4. Regular Expression to check input string is float
In this Python program example, we have defined a regular expression to check if the input string is a floating-point number. The regular expression checks the input number is float return True and False. We have used the Python re module search() method.
- We are asking the user to input a string floating-point number.
- Using re module search method that takes input number and a regular expression and returns True or False
- If the input number is a Float it will return True
- if the input number is not a Float it will return False.
import re
input_num = input("Please Enter input :")
regex = '[+-]?[0-9]+\.[0-9]+'
def check_num(input_num):
if(re.search(regex, input_num)):
print("Input is Floating point number")
else:
print("Input is not a Floating point number")
check_num(input_num)
Output
Please Enter input :"56.9"
Input is Floating point number
Summary
In this post, we have learned how to check if a number is float in Python by using python built-in function type(), isinstance(), and regular expression with python code example