If the string is convertable to integer, it should be digits only. It should be noted that this approach, as @cwallenpoole said, does NOT work with negative inputs beacuse of the '-' character. You could do:

if NumberString.isdigit():
    Number = int(NumberString)
else:
    Number = float(NumberString)

If you already have Number confirmed as a float, you can always use is_integer (works with negatives):

if Number.is_integer():
    Number = int(Number)
Answer from francisco sollima 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 - Explanation: Here, we check each variable a and b against both int and float. a is correctly identified as an integer and b as a float. isdigit() method is used to check if all characters in a string are digits.
Discussions

How to differentiate between a float and an integer in a string
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) More on reddit.com
๐ŸŒ r/learnpython
52
167
October 3, 2020
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 for a float?
EAFP: easier to ask forgiveness than permission. try to cast/convert the input to a float and except any errors. More on reddit.com
๐ŸŒ r/learnpython
44
1
December 17, 2022
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
People also ask

Is there a built-in Python method to check if a string is a number?
Python does not have a single built-in method to check if a string is a number.
๐ŸŒ
intellipaat.com
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
Why is it important to check if a string represents a number before performing mathematical operations?
Checking before performing mathematical operations is important because it ensures whether the string can be a number, which prevents runtime errors during execution.
๐ŸŒ
intellipaat.com
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
๐ŸŒ
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 ...
๐ŸŒ
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!

๐ŸŒ
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
If float(s) works but int(s) doesn't, it's a float. Anything else raises a ValueError. It's simple and reliable for most use cases. ... How can I fix 'Substituting the instance of a character, except the first, in a input string' in Python?
Find elsewhere
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-check-if-string-represents-number-in-python
How do I check if a string represents a number in Python? | Better Stack Community
February 3, 2023 - To check if a string represents a number (float or int) in Python, you can try casting it to a float or int and check if the cast was successful.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ check-if-a-string-is-a-float
Here is how to check if a string is a float in Python
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.
๐ŸŒ
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 ...
๐ŸŒ
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 - Use the float() function within a try-except block. Catch exceptions to handle strings that are not convertible to floats. ... This function tries to convert the string s to a float.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-check-for-float-string
Python - Check for float string - GeeksforGeeks
January 11, 2025 - In Python, the isdecimal() method is a quick and easy way to check if a string contains only decimal digits. It works by returning True when the string consists of digits from 0 to 9 and False otherwise.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-37288.html
Detecting float or int in a string
May 23, 2022 - The str.isnumeric() function is lacking when it comes to detecting floats. Several sites say to use a try/except to attempt the conversion. I was taught not to depend on try except in the regular program flow, and that it should only be used ...
Top answer
1 of 16
452

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.

๐ŸŒ
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.

๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ check if a string is a float in python
Check if a String is a Float in Python - Spark By {Examples}
May 21, 2024 - How do I check if a string is a number (float) in Python? You can use the float() function with try catch to check if a string is a float or not. In this
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Check If a Number Is an Integer in Python | note.nkmk.me
April 23, 2025 - Built-in Types - float.is_integer โ€” Python 3.13.3 documentation ยท f = 1.23 print(f.is_integer()) # False f_i = 100.0 print(f_i.is_integer()) # True ... For example, you can define a function that returns True for integer values (int or float with no fractional part). This function returns False for non-numeric types, such as strings (str).
๐ŸŒ
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.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ check-string-is-integer-python
How to Check if the String is Integer in Python
November 28, 2023 - Surprised by the Output? Instead of looking at whether the string is an integer, this method actually outputs whether a string contains any number, be it an integer or float or a mix of integers and strings.
๐ŸŒ
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 - Explanation: Here, it is checked if a string represents a valid number by allowing digits and only one decimal point. It returns True for the examples that satisfy the condition else returns False. isdigit(): This is a fast method when it comes to checking for a numeric query, but it works only for positive integers. try-except with float(): This method is more efficient and flexible since it handles integers, floats, and negatives quite well.