Python String isdecimal()

Filed Under: Python

Python String isdecimal() function returns True if all the characters in the string are decimal characters, otherwise False. If the string is empty then this function returns False.

python string isdecimal

Python String isdecimal()

If a character can be used to form a number in base 10, then it’s considered as decimal character. For example, ٠ (U+0660, ARABIC-INDIC DIGIT ZERO) and 𝟜 (U+1D7DC, MATHEMATICAL DOUBLE-STRUCK DIGIT FOUR) are also treated as decimal characters.

Let’s look at some of the examples of isdecimal() function.


s = '100'

print(s.isdecimal())

Output: True because all the characters in the string are decimals.


s = '0xF'
print(s.isdecimal())

Output: False because the string characters are not decimal and can’t be used to represent a number in base 10.


s = '10.55'
print(s.isdecimal())

Output: False because dot character is not a decimal character.


s = ''
print(s.isdecimal())

Output: False because string is empty.


s = '1٠2𝟜'  # U+0660, U+1D7DC
print(s.isdecimal())
print(int(s))

Output:


True
1024

Printing all Decimal characters in Python

We can use unicode module to check if a character is decimal or not. Here is the program to print all the decimal Unicode characters.


import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isdecimal():
        print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Decimal Unicode Characters = {count}')

Output:


...
ff15: 5 (FULLWIDTH DIGIT FIVE)
ff16: 6 (FULLWIDTH DIGIT SIX)
ff17: 7 (FULLWIDTH DIGIT SEVEN)
ff18: 8 (FULLWIDTH DIGIT EIGHT)
ff19: 9 (FULLWIDTH DIGIT NINE)
Total Number of Decimal Unicode Characters = 370

I have provided only partial output because the number of decimal Unicode characters is huge.

You can checkout more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages