What's the difference between str.isdigit, isnumeric and isdecimal in python?

前端 未结 5 1367
不知归路
不知归路 2020-11-27 13:21

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 (whic

5条回答
  •  眼角桃花
    2020-11-27 13:52

    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)
    

提交回复
热议问题