For non-negative (unsigned) integers only, use isdigit():

>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False

Documentation for isdigit(): Python2, Python3

For Python 2 Unicode strings: isnumeric().

Answer from Zoomulator on Stack Overflow
🌐
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!

Discussions

python - Checking to see if a string is an integer or float - Stack Overflow
So I'm creating a program to show number systems, however I've run into issues at the first hurdle. The program will take a number from the user and then use that number throughout the program in o... More on stackoverflow.com
🌐 stackoverflow.com
How can I check if a string represents an integer?
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. More on reddit.com
🌐 r/learnpython
12
4
April 7, 2021
python - How can I check if a string represents an int, without using try/except? - Stack Overflow
Is there any way to tell whether a string represents an integer (e.g., '3', '-17' but not '3.14' or 'asfasfas') Without using a try/except mechanism? is_int('3.14') == False is_int('-7') == True 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
People also ask

How can I check if a string represents a float in Python?
You can use a try-except block to convert the string to a float.
🌐
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
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
🌐
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 - a = '345.5' res = a.isdigit() if res == True: print("The number is an integer") else: print("The number is a float") ... Explanation: Although '345.5' represents a number, the presence of the decimal point (.) causes isdigit() to return False.
🌐
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 - 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 ...
🌐
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).
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
Note: I added a little extra feature, so you can check isint() with strings representing integers in any supported base, defaulting to base 10, of course. As you can see that trivial to support. In isfloat() I’m checking that the value is NOT an integer, since casting an integer to a float is perfectly legal in Python, but almost certainly not what you’d intend for such a check 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
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.
🌐
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.
🌐
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 - 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. Catch exceptions to handle strings that are not convertible ...
🌐
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.
🌐
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 call to the int() class succeeds, the string is an integer. If the except block runs, the string is a floating-point number.
🌐
Codecademy
codecademy.com › forum_questions › 5187c9af569b6ae7ab004fae
Did you write "if type(a) == int or float" and it's not working? See here | Codecademy
def distance_from_zero(a): if type(a) == int or type(a) == float: print abs(a) else: print 'Not an integer or a floating point decimal!' distance_from_zero(-5) And yes, substituting ‘return’ for ‘print’ works fine. So if the interpreter is happy, you should be too. Don’t worry about the Code Academy buggy error messages. ... Actually the problem is not the Python interpreter, the problem is the weakness and contained errors in the submission correctness test (SCT).
🌐
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
🌐
Flexiple
flexiple.com › python › check-if-int-python
How to Check if a String is an Integer in Python? - Flexiple
March 21, 2024 - To determine if a string is a number in Python, employing the try block along with the float function is a reliable method. This approach involves attempting to convert the string to a float using float(). If the conversion is successful, the ...
🌐
PYnative
pynative.com › home › python › check user input is a number or string in python
Python Check User input is a Number or String | Accept Numbers as input
April 24, 2021 - But when the user entered a number with some character in it (28Jessa), Python raised a ValueError exception because it is not int. Note: The isdigit() function will work only for positive integer numbers. i.e., if you pass any float number, ...
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.

🌐
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 - Python provides various methods to check if a string is an int or float such as isdigit(), float(), and int(). Learn more about these methods in detail with this blog!