How Can We Help?
Introduction
Python deals with a wide variety of data types. This includes numbers, strings, objects, etc. We generally treat the digits, numerals, and decimals as different names of the same entity. However, in Python, there are significant differences between the three. This article will explain the differences between the three functions: isdigit, isnumeric, and isdecimal.
The Major difference between isdigit vs isnumeric vs isdecimal is:
- isdigit function is built-in; it accepts a string and will only work when we enclose the numbers as a string.
- isnumeric function checks if the entire number is only numeric.
- isdecimal function checks if the given number is a decimal number or not.
And for simplicity, let’s see the below infographic:
Comparison Infographic :
Now let’s see in more detail how each one of these functions works.
How Does isdigit Work
Python isdigit is a built-in function. However, this will only work when we enclose the numbers in the form of a string. We know that Python does not treat numbers as objects. Hence we cannot apply the functions to the numbers. We can enclose the numbers both in single as well as double quotes.
Syntax:
<variable name>.isdigit()
Return value:
True if the variable is a digit but returns false if the variable is not a digit.
The method will generally return True if the variable is a digit and will return False otherwise.
Example (1)
num="11"
print(num.isdigit())
Output:
True
Example (2)
num="11.2"
print(num.isdigit())
Output:
False
Notice that although the above variable is a number, Python returns False. This is because the variable is a number not purely on digits but has decimals in between.
Example (3)
num="Hello World"
print(num.isdigit())
Output:
False
Special note:
Apart from pure numbers consisting of only digits, the function returns True if the character is a Unicode exponential character.
Example (4)
double = "\u00B2"
print(double)
double.isdigit()
cube = "\u00B3"
print(cube)
cube.isdigit()
Output:
²
³
True
Isnumeric Method:
There is a significant difference between Python’s isnumeric and the isdigit methods.
While the isdigit method checks if there are only digits in the string, the isnumeric method checks if the entire number is only numeric. So the isdigit has a domain that is a subset of the domain of the isnumeric method. If a variable returns True to the isdigit method, it must also return True in the isnumeric method, but the converse of the statement is not true.
Example (5)
num='111'
print(num.isdigit())
Output:
True
Example (6)
num='11.1'
print(num.isnumeric())
Output:
False
Example (7)
num='⅔'
print(num.isnumeric())
Output:
True
Example (8)
num='2²'
print(num.isnumeric())
Output:
True
We have to note that if we pass the fraction representation in the form of a string, the result will be False, and it will return True if we represent the fraction in the form of a Unicode representation.
Similarly, for the power, we have to represent it in the form of Unicode representation rather than a string to get the Truth value as True.
The function returns True if the given variable is a Roman Unicode number.
Example (9)
num='\u2167'
print(num.isnumeric())
Output:
True
In the above code, \u2167 is the Unicode representation of the Roman numerical iii; hence, the method returns True.
Python isdecimal Method
The isdecimal method is different from the previous two methods. This method checks if the given number is a decimal number.
The function returns True if all the numbers are evaluated to a base ten number.
Please note that the function will return False for the fractions and the exponents.
Example (10)
num='1.11'
print(num.isdecimal())
Output:
False
Example (11)
num='11'
print(num.isdecimal())
Output:
True
Example (12)
num='hello world'
print(num.isdecimal())
Output:
False
Until now, we have seen the usage of the three methods. You must have noticed that none of the three methods returns True for floating point numbers. However, the floating point numbers are significant, and we may need to check if a number is a floating point number too.
Example (13)
def check_float(n):
try:
float(n)
return True
except:
return False
lst=[133,1.1,2,2/3,'hello']
for i in lst:
if(check_float(i)):
print(f'{i} is a floating point number')
else:
print(f'{i} is not a floating point number')
Output:
133 is a floating point number
1.1 is a floating point number
2 is a floating point number
0.6666666666666666 is a floating point number
hello is not a floating point number
How to use isdigit, isnumeric, and isdecimal Methods With Negative Numbers
We can adopt several techniques to deal with negative numbers. One of the most efficient techniques would be trimming out the negative sign and applying the functions to the rest of the number.
Example (14)
def negative_check_isdigit(a_string):
if a_string[0] == '-':
return a_string[1:].isdigit()
else:
return a_string.isdigit()
lst=[133,1.1,2,2/3,'Hello']
for i in lst:
i=str(i)
if(negative_check_isdigit(i)):
print(f'{i} returns True for the isdigit function.')
else:
print(f'{i} returns False for the isdigit function.')
Output:
133 returns True for the isdigit function.
1.1 returns False for the isdigit function.
2 returns True for the isdigit function.
0.6666666666666666 returns False for the isdigit function.
Hello returns False for the isdigit function.
Example (15)
def negative_check_isdecimal(a_string):
if a_string[0] == '-':
return a_string[1:].isdecimal()
else:
return a_string.isdecimal()
lst=[133,1.1,2,2/3,'Hello']
for i in lst:
i=str(i)
if(negative_check_isdecimal(i)):
print(f'{i} returns True for the isdecimal function.')
else:
print(f'{i} returns False for the isdecimal function.')
Output:
133 returns True for the isdecimal function.
1.1 returns False for the isdecimal function.
2 returns True for the isdecimal function.
0.6666666666666666 returns False for the isdecimal function.
Hello returns False for the isdecimal function.
Example (16)
def negative_check_isnumeric(a_string):
if a_string[0] == '-':
return a_string[1:].isnumeric()
else:
return a_string.isnumeric()
lst=[133,1.1,2,2/3,'Hello']
for i in lst:
i=str(i)
if(negative_check_isnumeric(i)):
print(f'{i} returns True for the isnumeric function.')
else:
print(f'{i} returns False for the isnumeric function.')
Output:
133 returns True for the isnumeric function.
1.1 returns False for the isnumeric function.
2 returns True for the isnumeric function.
0.6666666666666666 returns False for the isnumeric function.
Hello returns False for the isnumeric function.
We can summarise all the above-discussed methods in one code using the class-based implementation as follows:
class Check:
def __init__(self,num):
self.num=num
def check_isdigit(self):
if self.num[0] == '-':
return self.num[1:].isdigit()
else:
return self.num.isdigit()
def check_isdeciamal(self):
if self.num[0] == '-':
return self.num[1:].isdecimal()
else:
return self.num.isdecimal()
def check_isnumeric(self):
self.num=str(self.num)
if self.num[0] == '-':
return self.num[1:].isnumeric()
else:
return self.num.isnumeric()
def main():
lst=[133,1.1,2,'⅔','Hello']
for i in lst:
check=Check(i)
if(check.check_isnumeric()):
print(f'{i} returns True for the isnumeric function.')
else:
print(f'{i} returns False for the isnumeric function.')
if(check.check_isdigit()):
print(f'{i} returns True for the isdigit function.')
else:
print(f'{i} returns False for the isdigit function.')
if(check.check_isdeciamal()):
print(f'{i} returns True for the isdecimal function.')
else:
print(f'{i} returns False for the isdecimal function.')
if __name__=='__main__':
main()
Output:
133 returns True for the isnumeric function.
133 returns True for the isdigit function.
133 returns True for the isdecimal function.
1.1 returns False for the isnumeric function.
1.1 returns False for the isdigit function.
1.1 returns False for the isdecimal function.
2 returns True for the isnumeric function.
2 returns True for the isdigit function.
2 returns True for the isdecimal function.
⅔ returns True for the isnumeric function.
⅔ returns False for the isdigit function.
⅔ returns False for the isdecimal function.
Hello returns False for the isnumeric function.
Hello returns False for the isdigit function.
Hello returns False for the isdecimal function.
Explanation:
- First, we created a class named Check. In this class, we first created a constructor. The class only takes one argument, called num.
- Under the class, we created three functions called check_isdigit, check_isdecimal, and check_isnumeric. Under each function, we defined the code to check whether the number passed returns True or False to the functions.
- Next, we created the main function, which is the driving code of our program.
- Under the main function, we first defined a list named lst. We defined a few elements under the list to check if they return True to the methods we discussed. We iterated through the list, and under each iteration, we first created the object named cheek. Next, we called the three functions under each iteration.
The below table will summarize all the examples we learned in tabular form.
Example | isdigit | isdecimal | isnumeric |
---|---|---|---|
‘0534’ | True | True | True |
‘123’ | True | True | True |
‘⅔’ | True | False | True |
‘²’ | True | False | True |
‘1.11’ | False | False | False |
‘Hello’ | False | False | False |
Conclusion:
In this article, we have learned the key differences between the isdigit, isnumeric, and isdecimal methods. We understood that although they deal with numbers, there are fundamental differences between the workings of those functions.
We strongly suggest that the readers try those functions with different types of numbers to understand the topic more.