Use isinstance.

Use with AI>>> x = 12
>>> isinstance(x, int)
True
>>> y = 12.0
>>> isinstance(y, float)
True

So:

Use with AI>>> if isinstance(x, int):
        print('x is a int!')

x is a int!

In case of long integers, the above won't work. So you need to do:

Use with AI>>> x = 12L
>>> import numbers
>>> isinstance(x, numbers.Integral)
True
>>> isinstance(x, int)
False
Answer from user225312 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-value-is-int-or-float-in-python
Check If Value Is Int or Float in Python - GeeksforGeeks
July 1, 2025 - In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance().
Discussions

python - check if an input is a float - Stack Overflow
I am starting learning Python and tried to build a simple calculator. I want to make my program check if number_1 and number_2 are really floats, and if they aren't, start the calculation again. Does More on stackoverflow.com
🌐 stackoverflow.com
Checking if a string can be converted to float in Python - Stack Overflow
I've got some Python code that runs through a list of strings and converts them to integers or floating point numbers if possible. Doing this for integers is pretty easy if element.isdigit(): More on stackoverflow.com
🌐 stackoverflow.com
How do I check if a number is a float number or whole number?
Conditional statements. Checking if a number is within range. ... Float and int in if else statment. ... I'm really new to python and wanted to share my first "project". More on reddit.com
🌐 r/learnpython
7
2
September 6, 2018
Float and int in if else statment.
I suppose you could compare value to its rounded value int(value): if value == int(value): ... By using /, value will always be a float so isinstance(value, int) won't be too useful. If you want value to stay a float, use integer division // instead. More on reddit.com
🌐 r/learnpython
10
3
September 6, 2022
🌐
Reddit
reddit.com › r/learnpython › how do i check for a float?
r/learnpython on Reddit: How do I check for a float?
December 17, 2022 -

while True:
while True:
print("Enter a number")
num1 = input("")
if (num1.isfloat()):
break
else:
continue
while True:
print("Enter a second number")
num2 = input("")
if (num2.isfloat()):
break
else:
continue
For context. I'm writing a super simple, two number calculator. And i wanted to write a section where it tests for a float. So when you type in a number, for number 1 (the variable is called num1), python is suppose to check if it is a float or not. And if it it, it will continue the script if it is. And ask for the number again if its not. However, I'm struggling to find the right syntax to check for a float. isdigit and numeric work, but for some reason isfloat() does not work for me. What am I typing wrong, I'm quite new to programming.

🌐
Programiz
programiz.com › python-programming › examples › check-string-number
Python Program to Check If a String Is a Number (Float) | Programiz
March 14, 2024 - If it is successful, then the function returns True. Else, ValueError is raised and returns False. For example, 's12' is alphanumeric, so it cannot be converted to float and False is returned; whereas, '1.123' is a numeric, so it is successfully converted to float.
🌐
PythonHow
pythonhow.com › how › check-if-a-string-is-a-float
Here is how to check if a string is a float in Python
New: Practice Python, JavaScript ... print(is_float('1.23a')) ... To check if a string is a number (float) in python, you can use isnumeric() in combination with the replace() method to check if the string can be casted to float or not....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-for-float-string
Python - Check for float string - GeeksforGeeks
July 11, 2025 - import re s = "123.45" pattern = r"^-?\d+\.\d+$" # Regular expression for a valid float if re.match(pattern, s): print("Valid float") else: print("Invalid float") ... regular expression r"^-?\d+\.\d+$" checks if the string s matches the pattern of a valid float, including optional negative signs.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Check If a Number Is an Integer in Python | note.nkmk.me
April 23, 2025 - The float type provides an is_integer() method, which returns True if the value is an integer and False otherwise. Built-in Types - float.is_integer — Python 3.13.3 documentation
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › how to check if a number is float or not in python
How to check if a number is float or not in Python - CodeSpeedy
November 25, 2019 - In python, it is very easy to check whether the number is float or not. Here are the few methods. Let’s start with type() in Python. ... In the above example, isinstance() is used to check whether the first argument is an object or ‘instance’ of the second argument or not. ... num = 34.22 if(num == float): print(‘This number is float’) else: print(‘This number is not float’)
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-a-variable-is-a-float-in-python-559600
How to Check If a Variable Is a Float in Python | LabEx
The type() function is a useful tool for understanding the data types you're working with in Python. In this step, you will learn how to use the isinstance() function in Python to check if a variable is of a specific type.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to check if a number is float in python
5 Best Ways to Check if a Number is Float in Python - Be on the Right Side of Change
February 16, 2024 - The expected output is a Boolean value: True if the number is a float, and False otherwise. An easy and direct way to check if a value is a float in Python is by using the built-in isinstance() function.
🌐
IncludeHelp
includehelp.com › python › how-do-i-check-if-a-string-is-a-number-float-in-python.aspx
How do I check if a string is a number (float) in Python?
April 28, 2025 - In this chapter, we will explore multiple methods to verify if a string is a float in Python. You can convert a string to a float using the float() function inside a try-except block to check if it is a valid float.
Top answer
1 of 16
456

I would just use..

try:
    float(element)
except ValueError:
    print("Not a float")

..it's simple, and it works. Note that it will still throw OverflowError if element is e.g. 1<<1024.

Another option would be a regular expression:

import re
if re.match(r'^-?\d+(?:\.\d+)$', element) is None:
    print("Not float")
2 of 16
325

Python3 method to check for float:

def is_float(element: any) -> bool:
    #If you expect None to be passed:
    if element is None: 
        return False
    try:
        float(element)
        return True
    except ValueError:
        return False

Python2 version of the above: How do I parse a string to a float or int?

Always do unit testing. What is and is not a float may surprise you:

Command to parse                        Is it a float?  Comment
--------------------------------------  --------------- ------------
print(isfloat(""))                      False
print(isfloat("1234567"))               True 
print(isfloat("1_2_3.4"))               True        123.4, underscores ignored
print(isfloat("NaN"))                   True        nan is also float
print(isfloat("123.456"))               True
print(isfloat("123.E4"))                True
print(isfloat(".1"))                    True
print(isfloat("6.523537535629999e-07")) True
print(isfloat("6e777777"))              True        This is same as Inf
print(isfloat("-iNF"))                  True
print(isfloat("1.797693e+308"))         True
print(isfloat("infinity"))              True
print(isfloat("1,234"))                 False
print(isfloat("NULL"))                  False       case insensitive
print(isfloat("NaNananana BATMAN"))     False
print(isfloat(",1"))                    False           
print(isfloat("123.EE4"))               False           
print(isfloat("infinity and BEYOND"))   False
print(isfloat("12.34.56"))              False       Two dots not allowed.
print(isfloat("#56"))                   False
print(isfloat("56%"))                   False
print(isfloat("0E0"))                   True
print(isfloat("x86E0"))                 False
print(isfloat("86-5"))                  False
print(isfloat("True"))                  False       Boolean is not a float.   
print(isfloat(True))                    True        Boolean is a float
print(isfloat("+1e1^5"))                False
print(isfloat("+1e1"))                  True
print(isfloat("+1e1.3"))                False
print(isfloat("+1.3P1"))                False
print(isfloat("-+1"))                   False
print(isfloat("(1)"))                   False       brackets not interpreted

Sinking exceptions like this is bad, because killing canaries is bad because the float method can fail for reasons other than user input. Do not be using code like this on life critical software. Also python has been changing its contract on what unicode strings can be promoted to float so expect this behavior of this code to change on major version updates.

🌐
Vultr
docs.vultr.com › python › examples › check if a string is a number (float)
Python Program to Check If a String Is a Number (Float) | Vultr Docs
December 6, 2024 - The regular expression checks for patterns that represent floats, including optional signs, decimal points, and scientific notation. It returns True if the string matches the pattern, indicating it could be a float. Determining whether a string is a float using Python involves straightforward techniques such as try-except blocks and can be enhanced with regular expression matching for greater accuracy.
🌐
Medium
medium.com › @davidmill1 › python-check-if-string-is-an-integer-or-float-aa122521c99f
Python — Check If String is an Integer or Float | by David Mill | Medium
December 24, 2022 - To overcome this problem, we need to modify your above code and use the try-catch method to check if the value can be parsed as float or integer or none. In the String class of Python, we have a function or method named isdecimal this method ...
🌐
Medium
medium.com › @Doug-Creates › validate-if-a-string-is-a-float-in-python-techniques-and-tips-e53004785ba8
Validate if a String is a Float in Python | by Doug Creates | Medium
March 21, 2024 - The process involves attempting ... the conversion fails, thus determining the string’s ability to represent a float. def is_float(value): """Check if the string is a float.""" try: float(value) # Try converting the string to ...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-check-if-number-is-int-or-float
Check if a number is an Integer or Float in Python | bobbyhadz
Use the `isinstance()` function to check if a number is an int or float, e.g. `if isinstance(my_num, int):`.
🌐
Codecademy
codecademy.com › forum_questions › 5187c9af569b6ae7ab004fae
Did you write "if type(a) == int or float" and it's not working? See here | Codecademy
Many people have been writing the following code to check if the number entered is an integer or a float · def distance_from_zero(a): if type(a) == int or float: .... But, if they enter something that isn’t a number they get an error. The reason is that this if statement will always be True. This is because of a concept called TRUTHVALUES. When Python has an if statement it checks the TRUTHVALUES of the statements.
🌐
Learn By Example
learnbyexample.org › how-to-check-if-a-number-is-an-integer-or-a-float-in-python
How to Check if a Number is an Integer or a Float in Python - Learn By Example
April 19, 2024 - You pass the variable you want to check to the type() function, and it returns its type. To determine if a number is an integer or float, you compare the result with int or float.
🌐
Python Guides
pythonguides.com › check-if-a-string-is-an-integer-or-float-in-python
How to Check if a String is an Integer or Float in Python?
March 19, 2025 - Learn how to check if a string is an integer or float in Python using methods like isdigit(), try-except blocks, and regular expressions, includes examples!