In our last few articles, we have been covering various methods for String handling in like checking if a string contains only alphabets or is alphanumeric characters, or checking if the string is in upper case or lower case, etc. Continuing ahead, in this article we will see how the isdigit()
, isdecimal()
, and isnumeric()
methods works.
1. The isdigit
method
This is a built-in method which checks the string for the presence of digits (0-9) in it. This method returns True, if the string contains only numbers. Also, the string should contain atleast one character, since an empty string returns False.
Syntax of isdigit
method
string.isdigit()
It doesn't take any parameters and the string refers to the string which has to be checked for containing digits. It returns True if all the characters in the string are digits and returns False if it is empty or has other characters like alphabets or special characters. Let's see a few use cases,
my_string = '123'
print(my_string.isdigit())
my_string = '123abc'
print(my_string.isdigit())
my_string = ''
print(my_string.isdigit())
Output:
True
False
False
Note:
-
If a parameter is passed to this method, it gives a TypeError
.
-
Roman numerals, fractions, and currencies are not considered as digits. Hence, this method will return False for such strings.
-
The subscript, superscript and decimal characters are considered to be digits. Hence, this method will return True for such strings.
2. The isnumeric
method
This is a built-in method which checks the string for the presence of numeric values. The numeric values refer to Unicode characters which includes integers, subscript, superscript, roman numerals, and fractions.
Syntax of isnumeric
method
string.isnumeric()
It doesn't take any parameters and the string refers to the string which has to be checked for containing numeric values. It returns True if the characters are numerical values and returns False, if the string is empty or has other characters like alphabets and special characters.
Time for an example:
my_string = '\u00BC'
print(my_string.isnumeric())
my_string = '123abx'
print(my_string.isnumeric())
my_string = '1230579'
print(my_string.isnumeric())
my_string = ''
print(my_string.isnumeric())
Output:
True
False
True
False
Note:
-
If a parameter is passed to this method, it gives a TypeError
.
-
Whitespaces are not numerics, hence returns False.
-
The integers, subscript, superscript, fractions, and roman numerals in Unicode are considered to be numerics. Hence, returns True.
3. The isdecimal
method
This method checks the characters for the presence of decimal numbers/characters.
Syntax of isdecimal
method
string.isdecimal()
It doesn't take any parameters and returns True if the string contains decimal numbers and False if the string is empty or if there are non decimal numbers present in the string. Let's take a code example,
my_string = '123780264'
print(my_string.isdecimal())
my_string = '12 34'
print(my_string.isdecimal())
my_string = ''
print(my_string.isdecimal())
Output:
True
False
False
Note:
-
If a parameter is passed to this method, it gives an Error
.
-
Whitespaces are not decimals, hence returns False.
-
The superscript, subscript, currency numerators, fractions, and roman numerals are not considered as decimal characters in Unicode. Hence, returns False.
Conclusion
In today's post, we understood how a string can be checked for the presence of digits, numeric characters, and decimal characters. Don't forget to meddle with the code to see what works and what doesn't.
You may also like: