Like some other comments have mentioned, you could cast every element in the list as a float, but can then use the float method is_integer() to check if the number was an integer. For example: numbers = '1 1.5 4.56 32' numbers = numbers.split(' ') integers = [int(x) for x in numbers if float(x).is_integer()] floats = [float(x) for x in numbers if not float(x).is_integer()] You mentioned a list, but showed a space separated string, in the event that you're actually working with a list of a mix of integers and floats, you can use python's built in isinstance() method: numbers = [1, 1.5, 4.56, 32] integers = [] floats = [] for number in numbers: if isinstance(number,int): integers.append(number) else: floats.append(number) Answer from ElliotDotpy on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to differentiate between a float and an integer in a string
r/learnpython on Reddit: How to differentiate between a float and an integer in a string
October 3, 2020 -

Hello everyone,

I have a very simple question.

Let's say you have a list:

'1 1.5 4.56 32'

And you want to separate the integers from the floats into different lists.

E.G.

int_list=[1,32]
float_list=[1.5,4.56]

I've tried a variety of things (convert it to a list and use try/except with int, but this only works with the integers, not the floats). Regex (\d+(?!\.)(?<!\.) basically, a digit that doesn't have a decimal before/after it, but this wouldn't work for the numbers past the 2nd decimal point). Only thing I've found is converting the string to an array and using as.type, but I want to do this without using numpy.

Any help would be greatly appreciated!

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i check if a string represents an integer?
r/learnpython on Reddit: How can I check if a string represents an integer?
April 7, 2021 -

I tried it with isinstance(<var>, int)

But if the string is: '5', it returns False because it's still a string.

If you write isinstance(int(<var>), int), in some cases it works but when the string is 'abc' the string cannot be casted into an integer and an error pops up. With type() it's the same problem.

With:

try:

int( '7.5')

except:

#code

7.5 can get casted into an integer but it's not an actual integer.

Top answer
1 of 6
8
See the various str methods : if yourvar.isdecimal(): A notable difference to using .isdigit you often see in examples is that also characters like ยฒ are considered a digit (so isdigit() will return True), while it isn't a decimal. Also you're example is not actually true: >>> int('7.5') Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '7.5' Because it can't be converted into an int at all, having a non-decimal character. What you're may confusing it with is providing a float to int() as that will simply use the object's integer part, making conversion transparent. That means that for strings you could actually use the try/except as a int-tester too, like in a custom function def is_int(val): try: int(val) return True except ValueError: return False if is_int(yourvar): As a sidenote about 'casting': casting is not in play in Python. Casting means you use the literal byte value(s) in memory in an operation meant for a different datatype. Say you have the ASCII string 'a' stored in memory, which is decimal value 97, then you could use that in a regular calculation to add, say 10 to it, resulting in the value 107. Reading that back as if it's a string, it will print the letter 'k'. In that way, the string variable is cast as an integer to perform the calculation. Without actually dealing with the string content ('a' is meaningless for a calculation like adding 10 to it), it's dealing with the literal 0 and 1 bits of the variable's data. In cases of using data in another representation, like '7' to be the integer 7, you are 'converting' or, specifically for strings, 'parsing' a value.
2 of 6
5
Try converting it to an integer, and catch the exception if it occurs.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ is there a way to check if a string is a number?
r/learnpython on Reddit: Is there a way to check if a string is a number?
August 9, 2024 -

Basically the title, is there any way to check if a string wiuld be able to be converted to a float without causing an error? Have a python program that involves inserting a lot of numbers in sucession and I would like to not have to start again every time I accidentally make a typo

๐ŸŒ
Medium
medium.com โ€บ @ravi.k7 โ€บ python-check-if-string-is-an-integer-or-float-aa122521c99f
Python โ€” Check If String is an Integer or Float | by Handan | Medium
December 24, 2022 - You can use the isdigit method of the String class to check if the given string input is a number or not. Suppose if the given input is not a number in that case this method will return false and hence based on the return of this method you ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Check If a Number Is an Integer in Python | note.nkmk.me
April 23, 2025 - def is_integer(n): try: float(n) except ValueError: return False else: return float(n).is_integer() print(is_integer(100)) # True print(is_integer(100.0)) # True print(is_integer(1.23)) # False print(is_integer('100')) # True print(is_integer('100.0')) # True print(is_integer('1.23')) # False print(is_integer('string')) # False ... See the following articles for more details on converting strings to numbers and handling exceptions using try ... except .... Convert a string to a number (int, float) in Python
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-check-if-a-string-is-int-or-float-in-Python
How to check if a string is int or float in Python - Quora
Answer (1 of 7): A string is a string. It is neither an int nor a float. It is a string. To check if a string can be converted into an int or a float, you can just do the conversion and deal with the consequences if it fails (ValueError). It will be much faster than trying to faff about with reg...
๐ŸŒ
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!!

๐ŸŒ
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 - It's commonly used for string inputs. ... a = '345.5' res = a.isdigit() if res == True: print("The number is an integer") else: print("The number is a float")
๐ŸŒ
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.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to check if a number entered into a program is an integer
r/learnpython on Reddit: How to check if a number entered into a program is an integer
October 14, 2020 -

Hi guys, I am an absolute beginner here working on a project for a college course.

I'm trying to write a program wherein a user enters a whole number between one and fifty, and the program simply tells the user whether their number is an even or an odd. Super basic stuff.

Now, I know that all user input is automatically considered a string, and I know how to convert the input to an integer within my code, but what I want is to check to see if what they entered was an integer (as opposed to a float) to begin with. If the user enters a number that includes decimals--let's say 12.3 as an example--I want to kick back an error message to them that tells them to use a whole number.

Here's my code as it stands right now:

num = input("Please enter a whole number between 1 and 50.")
if int(num) % 2 == 0 and int(num) >= 0 and int(num) <= 50:
    print("Your number is an even number")
elif int(num) % 2 == 1 and int(num) >= 0 and int(num) <= 50:
    print("Your number is an odd number")
else:
    print("Try again. Number must be between 1 and 50.")

As you can see, I've gone ahead and converted the input to integers in my code, so as it is right now, if the user enters a float, the program won't run and gives a value error. So how do I go about making sure the number entered is not a float? I tried:

if num != float 

But that's a syntax error and doesn't make any sense.

I'm sure it's something super obvious that I've just overlooked, but I am quite stumped. Thanks ahead of time for your help!

Top answer
1 of 2
2
You could try the isnumeric() function, it checks whether all characters of a string are 0-9. Returns false if there is a decimal point, so you can be sure it's an integer if it returns true. stringVarName.isnumeric()
2 of 2
2
if num != float: That isn't a syntax error, but it won't do what you're hoping as you've figured out. The right way to check if the type of something is `float is if isinstance(num, float): But that's not what you actually want in this case. As you said: input() always returns a string, and if you run some tests you'll find that int() only returns integers or raises exceptions. Try these out yourself: int("10") int("10.5") int("10.0") int("1/2") Or whatever else. Doing tests like this is a good way to confirm the behaviour of functions when you're not sure, and in this case you'll find it only ever returns int values or raises an exception. As such there's no point in checking if num is a float because it's guaranteed not to be; if the user enters a value with a decimal the first time int(num) is evaluated the exception will be raised. If you wanted to you could check if the user-entered value parses as a float to give the user a more useful error message. text = input("Please enter a whole number between 1 and 50.") try: num = int(text) # if it's not able to parse as an integer the `ValueError` exception will be caught below # and these checks don't run. if num % 2 == 0 and num >= 0 and num <= 50: ... ... except ValueError: num = float(text) # if the user entered total garbage the `float(text)` will just raise a new exception # and this code below will never run print("{} is not a whole number".format(num))
๐ŸŒ
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 & SQL with AI feedback โ€” Try ActiveSkill free โ†’ ร— ... def is_float(string): try: float(string) return True except ValueError: return False print(is_float('1.23')) print(is_float('123')) print(is_float('1.23a'))
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to think about integers, strings, and floats?
r/learnpython on Reddit: How to think about integers, strings, and floats?
June 17, 2023 -

So I am taking a intro to python class right now, I'm doing well in it and everything, but I guess I was asleep when they showed the slide telling the difference of integers, strings, and floats lol

I've never been "stuck" with anything because of it, and I am pretty good with excel so I understand stuff with VALUE() being necessary sometimes, as numbers (integers) can be stored as a string. I guess I just don't really know what float means. We've used it for stuff like

print(f"You are {age} years old!")

So I get that adding the f makes python know you are wanting to use that variable reference thing

What's a float?

๐ŸŒ
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
If the try block runs successfully, the string is an integer. If calling the int() class with the string raises a ValueError, the except block is run and the string is a floating-point number.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-8545.html
How to check if user entered string or integer or float??
February 25, 2018 - I am solving a basic example using while loop in which user enters a number and program prints a countdown from that number to zero. But the catch is that program will inform you if user enters an unexpected number or string. For checking if input nu...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ check-string-number
Python Program to Check If a String Is a Number (Float)
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')) ... Here, we have used try except in order to handle the ValueError ...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to check if a string is int or float in python?
How to check if a string is int or float in Python? - Intellipaat
February 2, 2026 - The string can be converted into a float or int number in Python by using the float/int function and, after itโ€™s converted, check whether you are getting conversion errors or not.