Python String isdigit() function returns True
if all the characters in the string are digits, otherwise False. If the string is empty then this function returns False.
Table of Contents
Python String isdigit()
Unicode digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. A digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
Let’s look at some of the examples of isdigit() function.
s = '100'
print(s.isdigit())
Output: True
because all the characters in the string are digits.
s = '0xF'
print(s.isdigit())
Output: False
because the string characters are not digits.
s = '10.55'
print(s.isdigit())
Output: False
because dot character is not a digit character.
s = ''
print(s.isdigit())
Output: False
because string is empty.
s = '1٠2?' # U+0660=0, U+1D7DC=4
print(s.isdigit())
print(int(s))
Output:
True
1024
Printing all Digit characters in Python
We can use unicode
module to check if a character is digit or not. Here is the program to print all the digit Unicode characters.
import unicodedata
count = 0
for codepoint in range(2 ** 16):
ch = chr(codepoint)
if ch.isdigit():
print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
count = count + 1
print(f'Total Number of Digit Unicode Characters = {count}')
Output:
...
ff16: 6 (FULLWIDTH DIGIT SIX)
ff17: 7 (FULLWIDTH DIGIT SEVEN)
ff18: 8 (FULLWIDTH DIGIT EIGHT)
ff19: 9 (FULLWIDTH DIGIT NINE)
Total Number of Digit Unicode Characters = 465
I have provided only partial output because the number of digit Unicode characters is huge.
Reference: Official Documentation