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 - There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to do this efficiently. type() function returns the type of the object you pass to it.
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
python - How do I check if a string represents a number (float or int)? - Stack Overflow
How do I check if a string represents a numeric value in Python? def is_number(s): try: float(s) return True except ValueError: return False The above works, but it... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to check if a user input is a float - Stack Overflow
When you call gold_room() at the ... will cause unexpected behavior if this is used and the user inputs a non float value. 2014-04-26T15:54:16.46Z+00:00 ... Save this answer. ... Show activity on this post. use below python based regex checking for float strings... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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 - To understand this example, you should have the knowledge of the following Python programming topics: ... def isfloat(num): try: float(num) return True except ValueError: return False print(isfloat('s12')) print(isfloat('1.123'))
๐ŸŒ
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โ€™)
๐ŸŒ
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 ยท f = 1.23 print(f.is_integer()) # False f_i = 100.0 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-check-for-float-string
Python - Check for float string - GeeksforGeeks
July 11, 2025 - Checking for float string refers to determining whether a given string can represent a floating-point number. A float string is a string that, when parsed, represents a valid float value, such as "3.14", "-2.0", or "0.001". ... try block attempts to create chunks of size k by zipping the string s into tuples and joining them. If any error occurs during this process, the except block catches it and prints an error message.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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 - The best method to use often depends on your specific use case. Letโ€™s briefly outline the common approaches: Using type(): This function is the most straightforward way to directly get the data type of any variable in Python.
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-check-if-a-number-is-a-float-in-Python
How to check if a number is a float in Python - Quora
Answer (1 of 18): [code]if isinstance(maybeFloat, float): print("It is float, amazing!") else: print("Nope, it is not a float.") [/code]Although if you do look up this question online, the most common answer is โ€œDonโ€™t check typesโ€. Python programmers really like hoping that their unit tes...
๐ŸŒ
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 - Through a series of examples, you'll ... value, enhancing the robustness and reliability of your data processing routines. Use the float() function within a try-except block....
๐ŸŒ
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 ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ float and int in if else statment.
r/learnpython on Reddit: Float and int in if else statment.
September 6, 2022 -

Hi!

Im wondering how to check if a value is a float or int using an if else statement.

E.g

test = int(input("Add a random number!"))
value = test / 4
if ..............?
print("Hello")
else:
print("Hello2") 

I have no idea if this is even close to being the right way to do it since i just started to learn Python and have no prior experience in programming.

Can someone please show me the way to do it since I can't find the answer on the internet!!

Top answer
1 of 15
592

To check if a float value is a whole number, use the float.is_integer() method:

>>> (1.0).is_integer()
True
>>> (1.555).is_integer()
False

The method was added to the float type in Python 2.6.

Take into account that in Python 2, 1/3 is 0 (floor division for integer operands!), and that floating point arithmetic can be imprecise (a float is an approximation using binary fractions, not a precise real number). But adjusting your loop a little this gives:

>>> for n in range(12000, -1, -1):
...     if (n ** (1.0/3)).is_integer():
...         print n
... 
27
8
1
0

which means that anything over 3 cubed, (including 10648) was missed out due to the aforementioned imprecision:

>>> (4**3) ** (1.0/3)
3.9999999999999996
>>> 10648 ** (1.0/3)
21.999999999999996

You'd have to check for numbers close to the whole number instead, or not use float() to find your number. Like rounding down the cube root of 12000:

>>> int(12000 ** (1.0/3))
22
>>> 22 ** 3
10648

If you are using Python 3.5 or newer, you can use the math.isclose() function to see if a floating point value is within a configurable margin:

>>> from math import isclose
>>> isclose((4**3) ** (1.0/3), 4)
True
>>> isclose(10648 ** (1.0/3), 22)
True

For older versions, the naive implementation of that function (skipping error checking and ignoring infinity and NaN) as mentioned in PEP485:

def isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
    return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
2 of 15
74

We can use the modulo (%) operator. This tells us how many remainders we have when we divide x by y - expresses as x % y. Every whole number must divide by 1, so if there is a remainder, it must not be a whole number.

This function will return a boolean, True or False, depending on whether n is a whole number.

def is_whole(n):
    return n % 1 == 0
๐ŸŒ
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 - You can use the re module with a regular expression pattern to check if a string matches the float format.