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

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
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
🌐
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")
🌐
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 ...
🌐
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.
🌐
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).
🌐
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.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
February 25, 2026 - Case is not significant, and there must be at least one hexadecimal digit in either the integer or the fraction. This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a format character or Java’s Double.toHexString are accepted by float.fromhex().
🌐
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.
🌐
YouTube
youtube.com › watch
How to check if a Python string is a number - float or int - YouTube
Try except block is the most Pythonic way to do this. We go through some alternatives and why they don't do everything you want.
Published   September 21, 2022
🌐
Newtum
blog.newtum.com › check-if-a-string-is-a-float-number-in-python
Learn Check if a String is a Float Number in Python
May 14, 2024 - By utilizing a try-except block and the float() function, we can easily determine the validity of the string conversion. This capability is particularly useful when validating user input or processing data that requires specific numeric formats.
🌐
GeeksforGeeks
geeksforgeeks.org › check-if-string-is-integer-in-python
Check If String is Integer in Python - GeeksforGeeks
April 2, 2025 - In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() ...
🌐
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
Most commonly the return value is the same as accessing the __class__ attribute on the object. ... Use the str.isdigit() method to check if every character in the string is a digit. If the method returns True, the string is an integer.
🌐
Linux Hint
linuxhint.com › python-check-string-float
Python Check if a String is a Float – Linux Hint
To check if a string is float or not in Python, the “float()” method, the replace()” method and the “isdigit()” method are used.
🌐
EyeHunts
tutorial.eyehunts.com › home › python check if string is integer or float
Python check if string is integer or float
July 27, 2023 - In the example above, we call the is_integer and is_float functions with different input strings to check if they represent an integer or a float, respectively. The functions return True if the input is a valid integer or float and False otherwise.
🌐
Sling Academy
slingacademy.com › article › python-check-if-a-string-can-be-converted-to-a-number
Python: Check If a String Can Be Converted to a Number - Sling Academy
June 4, 2023 - You can use the string isnumeric() method to verify that all the characters in a string are numeric (0-9) or other numeric characters like exponents (², ¾). This method returns True if the string is a numeric value, otherwise False. ... s = "2024" print(s.isnumeric()) # True s = "2²2" ...
🌐
Folkstalk
folkstalk.com › home › technology articles collection
Technology Articles Collection
July 3, 2025 - How to Convert Integer to String in Go Lang · How to Disable Unused Code Warnings in Rust with Example · Parsing and Formatting Date Time String in Go Lang Examples · How to check if a Map contains a Key in Go Lang · Golang Array Tutorial with examples ·
🌐
DataCamp
datacamp.com › community › tutorials › python-data-type-conversion
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - This is how you can convert an integer to a string in Python using the str() function: price_cake = 15 price_cookie = 6 total = price_cake + price_cookie print("The total is: " + str(total) + "$") ... It works the same way when you convert float to string values.