Python String isnumeric() Method
The str.isnumeric()
checks whether all the characters of the string are numeric characters or not. It will return True if all characters are numeric and will return False even if one character is non-numeric.
The numeric characters include digits (0 to 9) and Unicode numeric value, and fraction e.g. 'U+00B9', '½', etc.
Syntax:
str.isnumeric()
Parameters:
None
Return Value:
Boolean value True or False.
The following example checks for the numeric string.
>>> numstr = '100' #numeric digits
>>> numstr.isnumeric()
True
>>> numstr = '\u0034' # \u0034 is 4
>>> numstr.isnumeric()
True
>>> numstr = '\u00BD' # \u0024 is ½
>>> str.isnumeric()
True
>>> numstr = '¾'
>>> numstr.isnumeric()
True
In the above example, the isnumeric()
function returns True for all numeric values, Unicode for numric and vulgar fraction numeric such as 100, ½, '\u00BD', etc.
The isnumeric()
method returns false if encountered an alphabat, symbol, or an empty string, as shown below.
>>> str = '1A'
>>> str.isnumeric()
False
>>> str = '1.1'
>>> str.isnumeric()
False
>>> str = ''
>>>str.isnumeric()
False
>>> str = '#1'
>>>str.isnumeric()
False
>>> str = '$100'
>>>str.isnumeric()
False
The following table lists difference among the isdecimal(), isdigit(), and isnumeric()
methods based on the given inputs:
Input String Value | isdecimal() | isdigit() | isnumeric() |
---|---|---|---|
'123' | True | True | True |
'$123' | False | False | False |
'123.50' | False | False | False |
'123a' | False | False | False |
'¾' | False | False | True |
'\u0034' | True | True | True |