By default, Python reads every value from a file or form as a string, so "12" and "24.99" both show up as str. To check if a string is an integer or a float, try int() first, then float().
The order matters, because float("12") works too. If you call it first, every integer looks like a float.
In this tutorial, I’ll show you how to check if a string is an integer or float in Python. You’ll also see how to test for just one type and which odd strings slip through.
Integer vs Float in a Python String
An integer has no decimal point, like 12 or -3. A float has one, like 24.99, or uses an exponent, like 1e5.
We’ll use seven strings from an order sheet: "12" sold, "-3" returned, a price of "24.99", a rating of "5.0", plus "1,250", "N/A" and an empty string.
The last three are the tricky ones. Python can’t convert any of them as they are. For how floats are stored, see floating point numbers in Python.
Method 1: Check if a String Is an Integer or Float with try/except (Recommended)
Use this when the string could be either type, or not a number at all. The function returns "int", "float" or None.
- Create a file named
int_or_float.pyin VS Code. - Paste in this code and save the file.
- In the Command Prompt, run
python int_or_float.py. I used Python 3.12.5.
def number_type(text):
try:
int(text)
return "int"
except ValueError:
pass
try:
float(text)
return "float"
except ValueError:
return None
values = ["12", "-3", "24.99", "5.0", "1,250", "N/A", ""]
for text in values:
print(repr(text), number_type(text))
Output:
'12' int
'-3' int
'24.99' float
'5.0' float
'1,250' None
'N/A' None
'' None

How does this code work?
The int() function converts "12" and "-3", so the function returns "int" and stops there.
For "24.99", int() raises a ValueError. The code ignores it and moves on to the float() function, which succeeds.
"N/A" fails both, so you get None. If you only want to check if the string is a number of any kind, write if number_type(text):.
Method 2: Check if a String Is an Integer in Python
If only whole numbers are allowed, such as a quantity, you need just the int() half. Here are the same order sheet strings, without the last three, and isdigit() for comparison. Save it as is_int.py and run it the same way:
def is_int(text):
try:
int(text)
return True
except ValueError:
return False
for text in ["12", "-3", "24.99", "5.0"]:
print(repr(text), is_int(text), text.isdigit())
Output:
'12' True True
'-3' True False
'24.99' False False
'5.0' False False

How does this code work?
is_int() returns True if the conversion works. The two checks disagree on "-3": isdigit() says False, because the minus sign isn’t a digit.
So I don’t use isdigit() to find integers. It answers another question: does the string contain only numbers?
Is your value already a number and not text? Then check if the variable is an integer with isinstance().
Method 3: Check if a String Is a Float (Python Has No isfloat)
Python strings have isdigit() and isnumeric(), but there’s no isfloat(). You write it yourself, and first you decide whether "12" should count.
Here are both versions, with four strings from the same order sheet. Save this as is_float.py and run it:
def can_be_float(text):
try:
float(text)
return True
except ValueError:
return False
def is_int(text):
try:
int(text)
return True
except ValueError:
return False
def is_float(text):
return can_be_float(text) and not is_int(text)
for text in ["12", "24.99", "5.0", "N/A"]:
print(repr(text), can_be_float(text), is_float(text))
Output:
'12' True False
'24.99' True True
'5.0' True True
'N/A' False False

How does this code work?
can_be_float() is True for anything float() accepts, including "12". That’s what you want before doing math on a price.
is_float() also rejects integers, so "12" returns False. Pick this one when the two types need different handling.
If a value fails and you can’t see why, I cover the causes in could not convert string to float.
What Does is_integer() Do with “5.0”?
is_integer() tells you if a float is a whole number. It’s a method on numbers, not on strings, so "5.0".is_integer() raises an AttributeError.
Convert the string first. This file uses the same order sheet, now with just three values. Save it as whole_number.py and run it:
for text in ["12", "24.99", "5.0"]:
number = float(text)
if number.is_integer():
print(repr(text), "is a whole number:", int(number))
else:
print(repr(text), "has a decimal part")
Output:
'12' is a whole number: 12
'24.99' has a decimal part
'5.0' is a whole number: 5

How does this code work?
float("5.0") gives 5.0, and is_integer() returns True because nothing follows the decimal point. Now int(number) is safe and gives 5.
Don’t skip the float() step. int("5.0") raises a ValueError. There’s more in converting a string to an integer.
Which Odd Strings Count as an Integer or Float?
Python accepts more than you’d expect. This file runs the Method 1 function on ten strings that aren’t plain numbers. Save it as odd_strings.py and run it:
def number_type(text):
try:
int(text)
return "int"
except ValueError:
pass
try:
float(text)
return "float"
except ValueError:
return None
odd = [" 42 ", "1_000", "+3", "007", "\u0664\u0662",
"1e5", "nan", "inf", "$5", "12%"]
for text in odd:
print(ascii(text), number_type(text))
Output:
' 42 ' int
'1_000' int
'+3' int
'007' int
'\u0664\u0662' int
'1e5' float
'nan' float
'inf' float
'$5' None
'12%' None
The first five are all integers to Python, even with spaces around them or an underscore inside. The fifth is 42 in Arabic digits. ascii() prints it as escape codes.
"nan" and "inf" are valid floats as well. To reject them, test math.isfinite(float(text)) after import math. It returns False for both.
A currency or percent sign always gives None. So does the comma in "1,250" from our sample. Call text.replace(",", "") before the check, and it’s an integer.
Method 4: Use Regex for a Strict Integer or Float Check
Sometimes "1_000" or "nan" must not pass, for example in a price field. A regular expression lets you set the exact format.
I’ve mixed three order sheet values with four of the odd ones. Save this as strict_type.py and run it:
import re
def strict_type(text):
if re.fullmatch(r"[+-]?[0-9]+", text):
return "int"
if re.fullmatch(r"[+-]?[0-9]+\.[0-9]+", text):
return "float"
return None
values = ["12", "-3", "24.99", " 42 ", "1_000", "1e5", "nan"]
for text in values:
print(repr(text), strict_type(text))
Output:
'12' int
'-3' int
'24.99' float
' 42 ' None
'1_000' None
'1e5' None
'nan' None

How does this code work?
[+-]? allows one optional sign, and [0-9]+ needs at least one digit. The float pattern adds \. and more digits after it.
re.fullmatch() checks the whole string. I use [0-9] and not \d, because \d also matches digits from other alphabets.
This pattern rejects ".5" and "5.". To limit the decimal places too, see how to check for a number with a decimal.
Find Integer and Float Columns in a CSV File
Python’s csv module gives you strings only, so it’s a good place to use number_type(). Save these orders as orders.csv:
item,quantity,price,zip
Desk lamp,12,24.99,02108
Notebook,-3,5,60601
Monitor,5,189.50,78701
Then save this as csv_types.py in the same folder and run python csv_types.py:
import csv
def number_type(text):
try:
int(text)
return "int"
except ValueError:
pass
try:
float(text)
return "float"
except ValueError:
return None
def column_type(values):
types = {number_type(text) for text in values}
if None in types:
return "text"
return "float" if "float" in types else "int"
with open("orders.csv", newline="", encoding="utf-8") as file:
rows = list(csv.DictReader(file))
for name in rows[0]:
print(name, "->", column_type([row[name] for row in rows]))
Output:
item -> text
quantity -> int
price -> float
zip -> int

How does this code work?
column_type() collects the types found in a column. One non-number makes it text, and one float makes the whole column float. That’s why price is a float, even though one price is 5.
Important: the zip column counts as an integer, but don’t convert it.