str.isdecimal() and str.isdigit() difference example
Lets see some examples:
str.isdecimal() (Only Decimal Numbers)
Is 34 a decimal number? --> Yes
print("34".isdecimal()) #True
Is superscript 2 a decimal number? --> No
print("\u00B2")
print("\u00B2".isdecimal()) #False
str.isdigit() (Decimals, Subscripts, Superscripts)
Is 34 a digit? --> Yes
print("34".isdigit()) #True
Is superscript 2 a digit? --> Yes
print("\u00B2")
print("\u00B2".isdigit()) #True
str.isnumeric() (Decimals, Subscripts, Superscripts, Vulgar Fractions, Roman Numerals, Currency Numerators)
Is 34 a numeric number? --> Yes
print("34".isnumeric()) #True
Is superscript 2 a numeric number? --> Yes
print("\u00B2")
print("\u00B2".isnumeric()) #True
Is Vulgar Fraction one Quarter numeric number? -->Yes
print("\u00BC")
print("\u00BC".isnumeric()) #True
There are differences, but they're somewhat rare*. It mainly crops up with various unicode characters, such as 2
:
>>> c = '\u00B2'
>>> c.isdecimal()
False
>>> c.isdigit()
True
You can also go further down the careful-unicode-distinction rabbit hole with the isnumeric
method:
>>> c = '\u00BD' # ½
>>> c.isdecimal()
False
>>> c.isdigit()
False
>>> c.isnumeric()
True
*At least, I've never encountered production code that needs to distinguish between strings that contain different types of these exceptional situations, but surely use cases exist somewhere.
If you doubt, my advice - to code, to look a results, to draw conclusions.
A code
In [115]: import itertools
...:
...: line = '-' * 37
...: print(line)
...: print("| № | isdigit | isdecimal | chr")
...: print(line)
...: for number in itertools.chain(range(1000), range(4969, 4978), range(8304, 11000)):
...: char = chr(number)
...: if (char.isdigit() or char.isdecimal()):
...: print('| {0:>6} | {1:^7} | {2:^9} | {3:3} '.format(
...: number,
...: '+' if char.isdigit() else '-',
...: '+' if char.isdecimal() else '-',
...: char
...: )
...: )
...:
Look a results
-------------------------------------
| № | isdigit | isdecimal | chr
-------------------------------------
| 48 | + | + | 0
| 49 | + | + | 1
| 50 | + | + | 2
| 51 | + | + | 3
| 52 | + | + | 4
| 53 | + | + | 5
| 54 | + | + | 6
| 55 | + | + | 7
| 56 | + | + | 8
| 57 | + | + | 9
| 178 | + | - | ²
| 179 | + | - | ³
| 185 | + | - | ¹
| 4969 | + | - | ፩
| 4970 | + | - | ፪
| 4971 | + | - | ፫
| 4972 | + | - | ፬
| 4973 | + | - | ፭
| 4974 | + | - | ፮
| 4975 | + | - | ፯
| 4976 | + | - | ፰
| 4977 | + | - | ፱
| 8304 | + | - | ⁰
| 8308 | + | - | ⁴
| 8309 | + | - | ⁵
| 8310 | + | - | ⁶
| 8311 | + | - | ⁷
| 8312 | + | - | ⁸
| 8313 | + | - | ⁹
| 8320 | + | - | ₀
| 8321 | + | - | ₁
| 8322 | + | - | ₂
| 8323 | + | - | ₃
| 8324 | + | - | ₄
| 8325 | + | - | ₅
| 8326 | + | - | ₆
| 8327 | + | - | ₇
| 8328 | + | - | ₈
| 8329 | + | - | ₉
| 9312 | + | - | ①
| 9313 | + | - | ②
| 9314 | + | - | ③
| 9315 | + | - | ④
| 9316 | + | - | ⑤
| 9317 | + | - | ⑥
| 9318 | + | - | ⑦
| 9319 | + | - | ⑧
| 9320 | + | - | ⑨
| 9332 | + | - | ⑴
| 9333 | + | - | ⑵
| 9334 | + | - | ⑶
| 9335 | + | - | ⑷
| 9336 | + | - | ⑸
| 9337 | + | - | ⑹
| 9338 | + | - | ⑺
| 9339 | + | - | ⑻
| 9340 | + | - | ⑼
| 9352 | + | - | ⒈
| 9353 | + | - | ⒉
| 9354 | + | - | ⒊
| 9355 | + | - | ⒋
| 9356 | + | - | ⒌
| 9357 | + | - | ⒍
| 9358 | + | - | ⒎
| 9359 | + | - | ⒏
| 9360 | + | - | ⒐
| 9450 | + | - | ⓪
| 9461 | + | - | ⓵
| 9462 | + | - | ⓶
| 9463 | + | - | ⓷
| 9464 | + | - | ⓸
| 9465 | + | - | ⓹
| 9466 | + | - | ⓺
| 9467 | + | - | ⓻
| 9468 | + | - | ⓼
| 9469 | + | - | ⓽
| 9471 | + | - | ⓿
| 10102 | + | - | ❶
| 10103 | + | - | ❷
| 10104 | + | - | ❸
| 10105 | + | - | ❹
| 10106 | + | - | ❺
| 10107 | + | - | ❻
| 10108 | + | - | ❼
| 10109 | + | - | ❽
| 10110 | + | - | ❾
| 10112 | + | - | ➀
| 10113 | + | - | ➁
| 10114 | + | - | ➂
| 10115 | + | - | ➃
| 10116 | + | - | ➄
| 10117 | + | - | ➅
| 10118 | + | - | ➆
| 10119 | + | - | ➇
| 10120 | + | - | ➈
| 10122 | + | - | ➊
| 10123 | + | - | ➋
| 10124 | + | - | ➌
| 10125 | + | - | ➍
| 10126 | + | - | ➎
| 10127 | + | - | ➏
| 10128 | + | - | ➐
| 10129 | + | - | ➑
| 10130 | + | - | ➒
Draw a conclusions
As you can see, main difference between the function str.isdecimal() and str.isdigit() is that: the function str.isdecimal() return True only for numbers from 0 to 9, at the same time the function str.isdigit() return True for some other unicode-supported chars.