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 - 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'))
🌐
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.
🌐
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 ...
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....
🌐
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.
🌐
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.
🌐
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....
🌐
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...
🌐
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 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.
🌐
TutorialsPoint
tutorialspoint.com › python-ndash-check-for-float-string
Python – Check for float String
August 25, 2023 - def is_float_string(text): # If ... False '3.14' is float: True 'hello' is float: False '-7.5' is float: True · The try-except method is the most reliable approach for checking float strings as it leverages Python's built-in ...
🌐
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!!