Python: isdigit() vs. isdecimal()

See Python: Tips and Tricks for similar articles.

The difference between str.isdigit() and str.isdecimal() in Python is, for most of us, academic and can be used interchangeably. According to the Python documentation, the decimal category “includes digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.”

So, all decimals are digits, but not all digits are decimals. It turns out that most are though. Here’s a small Python script that returns the Unicode characters that are decimals but not digits.

import unicodedata
count=1
digitcount=0
decimalcount=0
print("#","Integer","Unicode Name","Character","HTML",sep="\t")
for i in range(1,1114111):
    if chr(i).isdigit() and not chr(i).isdecimal():
        try:
            print(count,i,unicodedata.name(chr(i)),chr(i), "" + str(i) + ";",sep="\t")
        except:
            print("??", "" + str(i) + ";",sep="\t")
        count=count+1
    if chr(i).isdigit():
        digitcount=digitcount+1
    if chr(i).isdecimal():
        decimalcount=decimalcount+1
    
print("digit count:",digitcount)
print("decimal count:",decimalcount)

Note that some of the characters cannot be printed out in the Python Shell or in the browser. The table shows both the character from the Python Shell (when possible) and the HTML char ref.

Python: Digits that are not Decimals
#IntegerUnicode NameCharacterHTML
1178SUPERSCRIPT TWO²²
2179SUPERSCRIPT THREE³³
3185SUPERSCRIPT ONE¹¹
44969ETHIOPIC DIGIT ONE
54970ETHIOPIC DIGIT TWO
64971ETHIOPIC DIGIT THREE
74972ETHIOPIC DIGIT FOUR
84973ETHIOPIC DIGIT FIVE
94974ETHIOPIC DIGIT SIX
104975ETHIOPIC DIGIT SEVEN
114976ETHIOPIC DIGIT EIGHT
124977ETHIOPIC DIGIT NINE
136618NEW TAI LUE THAM DIGIT ONE
148304SUPERSCRIPT ZERO
158308SUPERSCRIPT FOUR
168309SUPERSCRIPT FIVE
178310SUPERSCRIPT SIX
188311SUPERSCRIPT SEVEN
198312SUPERSCRIPT EIGHT
208313SUPERSCRIPT NINE
218320SUBSCRIPT ZERO
228321SUBSCRIPT ONE
238322SUBSCRIPT TWO
248323SUBSCRIPT THREE
258324SUBSCRIPT FOUR
268325SUBSCRIPT FIVE
278326SUBSCRIPT SIX
288327SUBSCRIPT SEVEN
298328SUBSCRIPT EIGHT
308329SUBSCRIPT NINE
319312CIRCLED DIGIT ONE
329313CIRCLED DIGIT TWO
339314CIRCLED DIGIT THREE
349315CIRCLED DIGIT FOUR
359316CIRCLED DIGIT FIVE
369317CIRCLED DIGIT SIX
379318CIRCLED DIGIT SEVEN
389319CIRCLED DIGIT EIGHT
399320CIRCLED DIGIT NINE
409332PARENTHESIZED DIGIT ONE
419333PARENTHESIZED DIGIT TWO
429334PARENTHESIZED DIGIT THREE
439335PARENTHESIZED DIGIT FOUR
449336PARENTHESIZED DIGIT FIVE
459337PARENTHESIZED DIGIT SIX
469338PARENTHESIZED DIGIT SEVEN
479339PARENTHESIZED DIGIT EIGHT
489340PARENTHESIZED DIGIT NINE
499352DIGIT ONE FULL STOP
509353DIGIT TWO FULL STOP
519354DIGIT THREE FULL STOP
529355DIGIT FOUR FULL STOP
539356DIGIT FIVE FULL STOP
549357DIGIT SIX FULL STOP
559358DIGIT SEVEN FULL STOP
569359DIGIT EIGHT FULL STOP
579360DIGIT NINE FULL STOP
589450CIRCLED DIGIT ZERO
599461DOUBLE CIRCLED DIGIT ONE
609462DOUBLE CIRCLED DIGIT TWO
619463DOUBLE CIRCLED DIGIT THREE
629464DOUBLE CIRCLED DIGIT FOUR
639465DOUBLE CIRCLED DIGIT FIVE
649466DOUBLE CIRCLED DIGIT SIX
659467DOUBLE CIRCLED DIGIT SEVEN
669468DOUBLE CIRCLED DIGIT EIGHT
679469DOUBLE CIRCLED DIGIT NINE
689471NEGATIVE CIRCLED DIGIT ZERO
6910102DINGBAT NEGATIVE CIRCLED DIGIT ONE
7010103DINGBAT NEGATIVE CIRCLED DIGIT TWO
7110104DINGBAT NEGATIVE CIRCLED DIGIT THREE
7210105DINGBAT NEGATIVE CIRCLED DIGIT FOUR
7310106DINGBAT NEGATIVE CIRCLED DIGIT FIVE
7410107DINGBAT NEGATIVE CIRCLED DIGIT SIX
7510108DINGBAT NEGATIVE CIRCLED DIGIT SEVEN
7610109DINGBAT NEGATIVE CIRCLED DIGIT EIGHT
7710110DINGBAT NEGATIVE CIRCLED DIGIT NINE
7810112DINGBAT CIRCLED SANS-SERIF DIGIT ONE
7910113DINGBAT CIRCLED SANS-SERIF DIGIT TWO
8010114DINGBAT CIRCLED SANS-SERIF DIGIT THREE
8110115DINGBAT CIRCLED SANS-SERIF DIGIT FOUR
8210116DINGBAT CIRCLED SANS-SERIF DIGIT FIVE
8310117DINGBAT CIRCLED SANS-SERIF DIGIT SIX
8410118DINGBAT CIRCLED SANS-SERIF DIGIT SEVEN
8510119DINGBAT CIRCLED SANS-SERIF DIGIT EIGHT
8610120DINGBAT CIRCLED SANS-SERIF DIGIT NINE
8710122DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT ONE
8810123DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT TWO
8910124DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT THREE
9010125DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT FOUR
9110126DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT FIVE
9210127DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT SIX
9310128DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT SEVEN
9410129DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT EIGHT
9510130DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT NINE
9668160KHAROSHTHI DIGIT ONE??𐩀
9768161KHAROSHTHI DIGIT TWO??𐩁
9868162KHAROSHTHI DIGIT THREE??𐩂
9968163KHAROSHTHI DIGIT FOUR??𐩃
10069216RUMI DIGIT ONE??𐹠
10169217RUMI DIGIT TWO??𐹡
10269218RUMI DIGIT THREE??𐹢
10369219RUMI DIGIT FOUR??𐹣
10469220RUMI DIGIT FIVE??𐹤
10569221RUMI DIGIT SIX??𐹥
10669222RUMI DIGIT SEVEN??𐹦
10769223RUMI DIGIT EIGHT??𐹧
10869224RUMI DIGIT NINE??𐹨
10969714BRAHMI NUMBER ONE??𑁒
11069715BRAHMI NUMBER TWO??𑁓
11169716BRAHMI NUMBER THREE??𑁔
11269717BRAHMI NUMBER FOUR??𑁕
11369718BRAHMI NUMBER FIVE??𑁖
11469719BRAHMI NUMBER SIX??𑁗
11569720BRAHMI NUMBER SEVEN??𑁘
11669721BRAHMI NUMBER EIGHT??𑁙
11769722BRAHMI NUMBER NINE??𑁚
118127232DIGIT ZERO FULL STOP??🄀
119127233DIGIT ZERO COMMA??🄁
120127234DIGIT ONE COMMA??🄂
121127235DIGIT TWO COMMA??🄃
122127236DIGIT THREE COMMA??🄄
123127237DIGIT FOUR COMMA??🄅
124127238DIGIT FIVE COMMA??🄆
125127239DIGIT SIX COMMA??🄇
126127240DIGIT SEVEN COMMA??🄈
127127241DIGIT EIGHT COMMA??🄉
128127242DIGIT NINE COMMA??🄊

Of the 1,114,111 Unicode characters, there are 588 digits, 460 of which are also decimals. So, there you are. 🙂

So, which should you use? It doesn’t make much difference really, but isdigit() is the most popular, perhaps because the name most closely matches the intention of the function.

Written by Nat Dunn. Follow Nat on Twitter.


Related Articles

  1. Fixing WebVTT Times with Python
  2. Using Python to Convert Images to WEBP
  3. Scientific Notation in Python
  4. Understanding Python’s __main__ variable
  5. Converting Leading Tabs to Spaces with Python
  6. pow(x, y, z) more efficient than x**y % z and other options
  7. A Python Model for Ping Pong Matches
  8. Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
  9. Python’s date.strftime() slower than str(), split, unpack, and concatenate?
  10. Basic Python Programming Exercise: A Penny Doubled Every Day
  11. Bi-directional Dictionary in Python
  12. How to find all your Python installations on Windows (and Mac)
  13. Associate Python Files with IDLE
  14. Change Default autosave Interval in JupyterLab
  15. Python: isdigit() vs. isdecimal() (this article)
  16. Python Clocks Explained
  17. Python Color Constants Module
  18. Maximum recursion depth exceeded while calling a Python object
  19. When to use Static Methods in Python? Never
  20. Finally, a use case for finally – Python Exception Handling
  21. Creating an Email Decorator with Python and AWS
  22. Python Coding Challenge: Two People with the Same Birthday
  23. How to Create a Simple Simulation in Python – Numeric Data
  24. Collatz Conjecture in Python
  25. Simple Python Script for Extracting Text from an SRT File
  26. Python Virtual Environments with venv
  27. Mapping python to Python 3 on Your Mac
  28. How to Make IDLE the Default Editor for Python Files on Windows
  29. How to Do Ternary Operator Assignment in Python
  30. How to Convert Seconds to Years with Python
  31. How to Create a Python Package
  32. How to Read a File with Python
  33. How to Check the Operating System with Python
  34. How to Use enumerate() to Print a Numbered List in Python
  35. How to Repeatedly Append to a String in Python
  36. Checking your Sitemap for Broken Links with Python
  37. How to do Simultaneous Assignment in Python
  38. Visual Studio Code - Opening Files with Python open()
  39. How to Slice Strings in Python
  40. How Python Finds Imported Modules
  41. How to Merge Dictionaries in Python
  42. How to Index Strings in Python
  43. How to Create a Tuple in Python