Fix Python – What’s the difference between str.isdigit, isnumeric and isdecimal in python?

Question

Asked By – matteo_c

When I run these methods

s.isdigit()
s.isnumeric()
s.isdecimal()

I always got as output or all True, or all False for each value of s (which is of course a string).
What’s​ the difference between the three? Can you provide an example that gives two Trues and one False (or vice versa)?

Now we will see solution for issue: What’s the difference between str.isdigit, isnumeric and isdecimal in python?


Answer

It’s mostly about unicode classifications. Here’s some examples to show discrepancies:

>>> def spam(s):
...     for attr in 'isnumeric', 'isdecimal', 'isdigit':
...         print(attr, getattr(s, attr)())
...         
>>> spam('½')
isnumeric True
isdecimal False
isdigit False
>>> spam('³')
isnumeric True
isdecimal False
isdigit True

Specific behaviour is in the official docs here.

Script to find all of them:

import sys
import unicodedata
from collections import defaultdict

d = defaultdict(list)
for i in range(sys.maxunicode + 1):
    s = chr(i)
    t = s.isnumeric(), s.isdecimal(), s.isdigit()
    if len(set(t)) == 2:
        try:
            name = unicodedata.name(s)
        except ValueError:
            name = f'codepoint{i}'
        print(s, name)
        d[t].append(s)

This question is answered By – wim

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0