It is often essential to check whether a string contains an integer, such as during input validation, data parsing, text analytics, etc. There are two possibilities: either the entire string comprises only of integers, or the string contains integers along with other characters. Multiple custom logics can be made for checking whether a string contains an integers or not.
In this Answer, we’ll learn how we can use the basic Python functions, the isdigit()
and isdecimal()
, to check if a string contains integers or not.
isdigit()
and isdecimal()
Both functions, the isdigit()
and isdecimal()
, are used to check for numbers within a given string. However, their functionality varies, as the isdecimal()
only looks for an integer. On the other hand, the isdigit()
can also return True
for superscripts and subscripts.
Thus, the function isdigit()
will return True
for any argument for which the function the isdecimal()
returns True
. However, the vice versa will not be the case as the isdecimal()
will return False
if a superscript or a subscript is found, but the isdigit()
would still return True
.
We can use both functions to check whether the complete string is numeric, where the whole string will be considered together, or check each string character separately to be a number. Let’s suppose we are checking whether the complete string contains only numbers. In that case, any single character returning False
will make the functions return False
, because that will mean a non-numeric character has been found. However, if we assess each character separately, then any character returning True
will make the functions return True
.
Going ahead, we’ll look at how both these functions work when a complete string is passed to them, and also when each character of a string is passed to them separately.
Let’s suppose we’re checking whether a string contains numbers without additional characters such as text, space, special characters, etc. In that case, the string can be passed to the function isdigit()
, or the function isdecimal()
, without any additional logic.
For a string str
, the syntax will be as follows:
str.function
In the above line of code, the function
will be either the isdigit()
or isdecimal()
.
The function isdigit()
will allow integers, superscripts and subscripts to be in the string but will return False
if any other character is found. On the other hand, the isdecimal()
will return False
if any character except an integer is found within the string.
Let’s see the following example to understand these better:
string = "234"print(f"The string is: '{string}'")print("Using 'isdigit()': ", string.isdigit())print("Using 'isdecimal()': ", string.isdecimal())print("-"*100)string1 = "2 3 4"print(f"The string is: '{string1}'")print("Using 'isdigit()': ", string1.isdigit())print("Using 'isdecimal()': ", string1.isdecimal())print("-"*100)string2 = "2\u00b3"print(f"The string is: '{string2}'")print("Using 'isdigit()': ", string2.isdigit())print("Using 'isdecimal()': ", string2.isdecimal())print("-"*100)string3 = "4\u2082"print(f"The string is: '{string3}'")print("Using 'isdigit()': ", string3.isdigit())print("Using 'isdecimal()': ", string3.isdecimal())
Lines 1–4: The string 234
contains integers only, so it returns True
for both functions.
Lines 8–11: The string 2 3 4
contains spaces as well, so it returns False
for both functions.
Lines 15–18: The string 2\u00b3
, in Python, displays \u00b3
is the superscript of 3
. The function isdecimal()
returns False
for the string, while the isdigit()
returns True
. This is because the isdecimal()
doesn’t recognize the superscript as an integer, but the isdigit()
does.
Lines 22–25: The string 4\u2082
, in Python, displays \u2082
is the superscript for 2
. The function isdecimal()
returns False
for the string, while the isdigit()
returns True
. This is because the isdecimal()
doesn’t recognize the subscript as an integer, but the isdigit()
does.
When the goal is to check whether a string contains integers only, without superscripts or subscripts being recognized, then the isdecimal()
should be used with the complete string.
However, if the goal is to check whether the string contains integers only, but with superscripts and subscripts of integers counted as well, then the isdigit()
should be used.
If we’re checking whether a string contains integers, with or without other characters, then slight changes can be made to the code. These slight changes can be implemented, so that instead of the whole string each character can be inspected for being an integer.
To check each character of the string str
, the syntax will be as follows:
any(letter.function for letter in str)
In the above line of code, the function
will be either the isdigit()
or the isdecimal
, while the letter
is an arbitrary variable name.
If the function the isdigit()
is used above, the code will return True
if any integer, superscript or subscript is found within the string. However, if the function the isdecimal()
is used, the code will return True
only if an integer is found within the code.
Let’s see the following example to understand this better.
string = "I have 2 dogs"print(f"The string is: '{string}'")print("Using 'isdigit()': ", any(digit.isdigit() for digit in string))print("Using 'isdecimal()': ", any(dec.isdecimal() for dec in string))print("-"*100)string1 = "I have some money"print(f"The string is: '{string1}'")print("Using 'isdigit()': ", any(digit.isdigit() for digit in string1))print("Using 'isdecimal()': ", any(dec.isdecimal() for dec in string1))print("-"*100)string2 = "I used H\u2082SO\u2084 in fertilizer processing"print(f"The string is: '{string2}'")print("Using 'isdigit()': ", any(digit.isdigit() for digit in string2))print("Using 'isdecimal()': ", any(dec.isdecimal() for dec in string2))
Lines 1–4: The string I have 2 dogs
contains the integer 2
, so it returns True
for both functions.
Lines 8–11: The string I have some money
contains no integer at all, so it returns False
for both functions.
Lines 15–18: The string I used H\u2082SO\u2084 in fertilizer processing
, in Python, displays I used H₂SO₄ in fertilizer processing
with 2 subscripts used. The function the isdigit()
recognizes the subscripts as integers, so the code using this function returns True
. The function the isdecimal()
doesn’t recognize the subscripts as integers, so the code using this function returns False
.
When the goal is to check whether a string contains an integer at all, without superscripts or subscripts being recognized, then the isdecimal()
should be used for checking each character.
Otherwise, if the goal is to check whether the string contains any integer, but with superscripts subscripts of integers counted as well, the isdigit()
should be used for checking each character.
There are multiple methods to check if a string contains integers. We used two of the built-in functions of Python for this purpose, and explained their differences in usage.
Free Resources