Use isinstance.

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

So:

>>> 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:

>>> 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
python - How to check if a user input is a float - Stack Overflow
I'm doing Learn Python the Hard Way exercise 35. Below is the original code, and we're asked to change it so it can accept numbers that don't have just 0 and 1 in them. def gold_room(): print ... 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
🌐
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....
🌐
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
🌐
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.
Find elsewhere
🌐
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.
🌐
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’)
🌐
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.
🌐
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 ...
🌐
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.
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.

🌐
Linux Hint
linuxhint.com › python-check-string-float
Python Check if a String is a Float – Linux Hint
To check if a string is float or not in Python, the “float()” method, the “replace()” method and the “isdigit()” method are used.
🌐
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 ...
🌐
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.
🌐
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):`.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 effective ways to check if a string is a float in python
5 Effective Ways to Check if a String is a Float in Python - Be on the Right Side of Change
February 14, 2024 - A match returns True, indicating a valid float; no match returns False. Python’s str.isdigit() function can be used in combination with string splitting to check if a string could be a float.