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.
# | Integer | Unicode Name | Character | HTML |
---|---|---|---|---|
1 | 178 | SUPERSCRIPT TWO | ² | ² |
2 | 179 | SUPERSCRIPT THREE | ³ | ³ |
3 | 185 | SUPERSCRIPT ONE | ¹ | ¹ |
4 | 4969 | ETHIOPIC DIGIT ONE | ፩ | ፩ |
5 | 4970 | ETHIOPIC DIGIT TWO | ፪ | ፪ |
6 | 4971 | ETHIOPIC DIGIT THREE | ፫ | ፫ |
7 | 4972 | ETHIOPIC DIGIT FOUR | ፬ | ፬ |
8 | 4973 | ETHIOPIC DIGIT FIVE | ፭ | ፭ |
9 | 4974 | ETHIOPIC DIGIT SIX | ፮ | ፮ |
10 | 4975 | ETHIOPIC DIGIT SEVEN | ፯ | ፯ |
11 | 4976 | ETHIOPIC DIGIT EIGHT | ፰ | ፰ |
12 | 4977 | ETHIOPIC DIGIT NINE | ፱ | ፱ |
13 | 6618 | NEW TAI LUE THAM DIGIT ONE | ᧚ | ᧚ |
14 | 8304 | SUPERSCRIPT ZERO | ⁰ | ⁰ |
15 | 8308 | SUPERSCRIPT FOUR | ⁴ | ⁴ |
16 | 8309 | SUPERSCRIPT FIVE | ⁵ | ⁵ |
17 | 8310 | SUPERSCRIPT SIX | ⁶ | ⁶ |
18 | 8311 | SUPERSCRIPT SEVEN | ⁷ | ⁷ |
19 | 8312 | SUPERSCRIPT EIGHT | ⁸ | ⁸ |
20 | 8313 | SUPERSCRIPT NINE | ⁹ | ⁹ |
21 | 8320 | SUBSCRIPT ZERO | ₀ | ₀ |
22 | 8321 | SUBSCRIPT ONE | ₁ | ₁ |
23 | 8322 | SUBSCRIPT TWO | ₂ | ₂ |
24 | 8323 | SUBSCRIPT THREE | ₃ | ₃ |
25 | 8324 | SUBSCRIPT FOUR | ₄ | ₄ |
26 | 8325 | SUBSCRIPT FIVE | ₅ | ₅ |
27 | 8326 | SUBSCRIPT SIX | ₆ | ₆ |
28 | 8327 | SUBSCRIPT SEVEN | ₇ | ₇ |
29 | 8328 | SUBSCRIPT EIGHT | ₈ | ₈ |
30 | 8329 | SUBSCRIPT NINE | ₉ | ₉ |
31 | 9312 | CIRCLED DIGIT ONE | ① | ① |
32 | 9313 | CIRCLED DIGIT TWO | ② | ② |
33 | 9314 | CIRCLED DIGIT THREE | ③ | ③ |
34 | 9315 | CIRCLED DIGIT FOUR | ④ | ④ |
35 | 9316 | CIRCLED DIGIT FIVE | ⑤ | ⑤ |
36 | 9317 | CIRCLED DIGIT SIX | ⑥ | ⑥ |
37 | 9318 | CIRCLED DIGIT SEVEN | ⑦ | ⑦ |
38 | 9319 | CIRCLED DIGIT EIGHT | ⑧ | ⑧ |
39 | 9320 | CIRCLED DIGIT NINE | ⑨ | ⑨ |
40 | 9332 | PARENTHESIZED DIGIT ONE | ⑴ | ⑴ |
41 | 9333 | PARENTHESIZED DIGIT TWO | ⑵ | ⑵ |
42 | 9334 | PARENTHESIZED DIGIT THREE | ⑶ | ⑶ |
43 | 9335 | PARENTHESIZED DIGIT FOUR | ⑷ | ⑷ |
44 | 9336 | PARENTHESIZED DIGIT FIVE | ⑸ | ⑸ |
45 | 9337 | PARENTHESIZED DIGIT SIX | ⑹ | ⑹ |
46 | 9338 | PARENTHESIZED DIGIT SEVEN | ⑺ | ⑺ |
47 | 9339 | PARENTHESIZED DIGIT EIGHT | ⑻ | ⑻ |
48 | 9340 | PARENTHESIZED DIGIT NINE | ⑼ | ⑼ |
49 | 9352 | DIGIT ONE FULL STOP | ⒈ | ⒈ |
50 | 9353 | DIGIT TWO FULL STOP | ⒉ | ⒉ |
51 | 9354 | DIGIT THREE FULL STOP | ⒊ | ⒊ |
52 | 9355 | DIGIT FOUR FULL STOP | ⒋ | ⒋ |
53 | 9356 | DIGIT FIVE FULL STOP | ⒌ | ⒌ |
54 | 9357 | DIGIT SIX FULL STOP | ⒍ | ⒍ |
55 | 9358 | DIGIT SEVEN FULL STOP | ⒎ | ⒎ |
56 | 9359 | DIGIT EIGHT FULL STOP | ⒏ | ⒏ |
57 | 9360 | DIGIT NINE FULL STOP | ⒐ | ⒐ |
58 | 9450 | CIRCLED DIGIT ZERO | ⓪ | ⓪ |
59 | 9461 | DOUBLE CIRCLED DIGIT ONE | ⓵ | ⓵ |
60 | 9462 | DOUBLE CIRCLED DIGIT TWO | ⓶ | ⓶ |
61 | 9463 | DOUBLE CIRCLED DIGIT THREE | ⓷ | ⓷ |
62 | 9464 | DOUBLE CIRCLED DIGIT FOUR | ⓸ | ⓸ |
63 | 9465 | DOUBLE CIRCLED DIGIT FIVE | ⓹ | ⓹ |
64 | 9466 | DOUBLE CIRCLED DIGIT SIX | ⓺ | ⓺ |
65 | 9467 | DOUBLE CIRCLED DIGIT SEVEN | ⓻ | ⓻ |
66 | 9468 | DOUBLE CIRCLED DIGIT EIGHT | ⓼ | ⓼ |
67 | 9469 | DOUBLE CIRCLED DIGIT NINE | ⓽ | ⓽ |
68 | 9471 | NEGATIVE CIRCLED DIGIT ZERO | ⓿ | ⓿ |
69 | 10102 | DINGBAT NEGATIVE CIRCLED DIGIT ONE | ❶ | ❶ |
70 | 10103 | DINGBAT NEGATIVE CIRCLED DIGIT TWO | ❷ | ❷ |
71 | 10104 | DINGBAT NEGATIVE CIRCLED DIGIT THREE | ❸ | ❸ |
72 | 10105 | DINGBAT NEGATIVE CIRCLED DIGIT FOUR | ❹ | ❹ |
73 | 10106 | DINGBAT NEGATIVE CIRCLED DIGIT FIVE | ❺ | ❺ |
74 | 10107 | DINGBAT NEGATIVE CIRCLED DIGIT SIX | ❻ | ❻ |
75 | 10108 | DINGBAT NEGATIVE CIRCLED DIGIT SEVEN | ❼ | ❼ |
76 | 10109 | DINGBAT NEGATIVE CIRCLED DIGIT EIGHT | ❽ | ❽ |
77 | 10110 | DINGBAT NEGATIVE CIRCLED DIGIT NINE | ❾ | ❾ |
78 | 10112 | DINGBAT CIRCLED SANS-SERIF DIGIT ONE | ➀ | ➀ |
79 | 10113 | DINGBAT CIRCLED SANS-SERIF DIGIT TWO | ➁ | ➁ |
80 | 10114 | DINGBAT CIRCLED SANS-SERIF DIGIT THREE | ➂ | ➂ |
81 | 10115 | DINGBAT CIRCLED SANS-SERIF DIGIT FOUR | ➃ | ➃ |
82 | 10116 | DINGBAT CIRCLED SANS-SERIF DIGIT FIVE | ➄ | ➄ |
83 | 10117 | DINGBAT CIRCLED SANS-SERIF DIGIT SIX | ➅ | ➅ |
84 | 10118 | DINGBAT CIRCLED SANS-SERIF DIGIT SEVEN | ➆ | ➆ |
85 | 10119 | DINGBAT CIRCLED SANS-SERIF DIGIT EIGHT | ➇ | ➇ |
86 | 10120 | DINGBAT CIRCLED SANS-SERIF DIGIT NINE | ➈ | ➈ |
87 | 10122 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT ONE | ➊ | ➊ |
88 | 10123 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT TWO | ➋ | ➋ |
89 | 10124 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT THREE | ➌ | ➌ |
90 | 10125 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT FOUR | ➍ | ➍ |
91 | 10126 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT FIVE | ➎ | ➎ |
92 | 10127 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT SIX | ➏ | ➏ |
93 | 10128 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT SEVEN | ➐ | ➐ |
94 | 10129 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT EIGHT | ➑ | ➑ |
95 | 10130 | DINGBAT NEGATIVE CIRCLED SANS-SERIF DIGIT NINE | ➒ | ➒ |
96 | 68160 | KHAROSHTHI DIGIT ONE | ?? | 𐩀 |
97 | 68161 | KHAROSHTHI DIGIT TWO | ?? | 𐩁 |
98 | 68162 | KHAROSHTHI DIGIT THREE | ?? | 𐩂 |
99 | 68163 | KHAROSHTHI DIGIT FOUR | ?? | 𐩃 |
100 | 69216 | RUMI DIGIT ONE | ?? | 𐹠 |
101 | 69217 | RUMI DIGIT TWO | ?? | 𐹡 |
102 | 69218 | RUMI DIGIT THREE | ?? | 𐹢 |
103 | 69219 | RUMI DIGIT FOUR | ?? | 𐹣 |
104 | 69220 | RUMI DIGIT FIVE | ?? | 𐹤 |
105 | 69221 | RUMI DIGIT SIX | ?? | 𐹥 |
106 | 69222 | RUMI DIGIT SEVEN | ?? | 𐹦 |
107 | 69223 | RUMI DIGIT EIGHT | ?? | 𐹧 |
108 | 69224 | RUMI DIGIT NINE | ?? | 𐹨 |
109 | 69714 | BRAHMI NUMBER ONE | ?? | 𑁒 |
110 | 69715 | BRAHMI NUMBER TWO | ?? | 𑁓 |
111 | 69716 | BRAHMI NUMBER THREE | ?? | 𑁔 |
112 | 69717 | BRAHMI NUMBER FOUR | ?? | 𑁕 |
113 | 69718 | BRAHMI NUMBER FIVE | ?? | 𑁖 |
114 | 69719 | BRAHMI NUMBER SIX | ?? | 𑁗 |
115 | 69720 | BRAHMI NUMBER SEVEN | ?? | 𑁘 |
116 | 69721 | BRAHMI NUMBER EIGHT | ?? | 𑁙 |
117 | 69722 | BRAHMI NUMBER NINE | ?? | 𑁚 |
118 | 127232 | DIGIT ZERO FULL STOP | ?? | 🄀 |
119 | 127233 | DIGIT ZERO COMMA | ?? | 🄁 |
120 | 127234 | DIGIT ONE COMMA | ?? | 🄂 |
121 | 127235 | DIGIT TWO COMMA | ?? | 🄃 |
122 | 127236 | DIGIT THREE COMMA | ?? | 🄄 |
123 | 127237 | DIGIT FOUR COMMA | ?? | 🄅 |
124 | 127238 | DIGIT FIVE COMMA | ?? | 🄆 |
125 | 127239 | DIGIT SIX COMMA | ?? | 🄇 |
126 | 127240 | DIGIT SEVEN COMMA | ?? | 🄈 |
127 | 127241 | DIGIT EIGHT COMMA | ?? | 🄉 |
128 | 127242 | DIGIT 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.
Related Articles
- Fixing WebVTT Times with Python
- Using Python to Convert Images to WEBP
- Scientific Notation in Python
- Understanding Python’s __main__ variable
- Converting Leading Tabs to Spaces with Python
- pow(x, y, z) more efficient than x**y % z and other options
- A Python Model for Ping Pong Matches
- Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
- Python’s date.strftime() slower than str(), split, unpack, and concatenate?
- Basic Python Programming Exercise: A Penny Doubled Every Day
- Bi-directional Dictionary in Python
- How to find all your Python installations on Windows (and Mac)
- Associate Python Files with IDLE
- Change Default autosave Interval in JupyterLab
- Python: isdigit() vs. isdecimal() (this article)
- Python Clocks Explained
- Python Color Constants Module
- Maximum recursion depth exceeded while calling a Python object
- When to use Static Methods in Python? Never
- Finally, a use case for finally – Python Exception Handling
- Creating an Email Decorator with Python and AWS
- Python Coding Challenge: Two People with the Same Birthday
- How to Create a Simple Simulation in Python – Numeric Data
- Collatz Conjecture in Python
- Simple Python Script for Extracting Text from an SRT File
- Python Virtual Environments with venv
- Mapping python to Python 3 on Your Mac
- How to Make IDLE the Default Editor for Python Files on Windows
- How to Do Ternary Operator Assignment in Python
- How to Convert Seconds to Years with Python
- How to Create a Python Package
- How to Read a File with Python
- How to Check the Operating System with Python
- How to Use enumerate() to Print a Numbered List in Python
- How to Repeatedly Append to a String in Python
- Checking your Sitemap for Broken Links with Python
- How to do Simultaneous Assignment in Python
- Visual Studio Code - Opening Files with Python open()
- How to Slice Strings in Python
- How Python Finds Imported Modules
- How to Merge Dictionaries in Python
- How to Index Strings in Python
- How to Create a Tuple in Python