Python String isdigit() Method
The isdigit()
method returns True if all characters in a string are digits or Unicode char of a digit. If not, it returns False.
Syntax:
str.isdigit()
Parameters:
None.
Return Value:
Returns True if all characters in the string are digits and returns False even if one character is not a digit.
The following examples demonstrate isdigit()
method.
mystr = '12345'
print(mystr.isdigit())
mystr = '10.5'
print(mystr.isdigit())
mystr = 'python'
print(mystr.isdigit())
True
False
False
The isdigit()
method will return False if the string contains whitespaces, symbols, or alphabets.
print('$100'.isdigit())
print('100 '.isdigit())
False
False
Superscript and subscripts (usually written using Unicode) are also considered digit characters. Hence, if the string contains these decimal characters, the isdigit()
returns True. The mentioned Unicode characters should have a numerical output.
If the Unicode values do not return a digit, then isdigit()
method returns False.
num = '\u0038' # Unicode char for 8
print(num.isdigit())
mystr = '\u00380061' # Unicode char 8a
print(mystr.isdigit())
arabic_six = '۶' # Arabic digit 6
print(arabic_six.isdigit())
True
False
True
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 |