str.isdecimal() and str.isdigit() difference example
Solution 1:
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.
Solution 2:
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
Solution 3:
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.
Solution 4:
Completing the answer from PADYMKO, we can add the other function of interest and extend the code
Code
import itertools
line = '-' * 49
print(line)
print("| № | isdigit | isdecimal | isnumeric | 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() or char.isnumeric()):
print('| {0:>6} | {1:^7} | {2:^9} | {3:^9} | {4:3} |'.format(
number,
'+' if char.isdigit() else '-',
'+' if char.isdecimal() else '-',
'+' if char.isnumeric() else '-',
char
)
)
And the results
-------------------------------------------------
| № | isdigit | isdecimal | isnumeric | chr |
-------------------------------------------------
| 48 | + | + | + | 0 |
| 49 | + | + | + | 1 |
| 50 | + | + | + | 2 |
| 51 | + | + | + | 3 |
| 52 | + | + | + | 4 |
| 53 | + | + | + | 5 |
| 54 | + | + | + | 6 |
| 55 | + | + | + | 7 |
| 56 | + | + | + | 8 |
| 57 | + | + | + | 9 |
| 178 | + | - | + | ² |
| 179 | + | - | + | ³ |
| 185 | + | - | + | ¹ |
| 188 | - | - | + | ¼ |
| 189 | - | - | + | ½ |
| 190 | - | - | + | ¾ |
| 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 | + | - | + | ₉ |
| 8528 | - | - | + | ⅐ |
| 8529 | - | - | + | ⅑ |
| 8530 | - | - | + | ⅒ |
| 8531 | - | - | + | ⅓ |
| 8532 | - | - | + | ⅔ |
| 8533 | - | - | + | ⅕ |
| 8534 | - | - | + | ⅖ |
| 8535 | - | - | + | ⅗ |
| 8536 | - | - | + | ⅘ |
| 8537 | - | - | + | ⅙ |
| 8538 | - | - | + | ⅚ |
| 8539 | - | - | + | ⅛ |
| 8540 | - | - | + | ⅜ |
| 8541 | - | - | + | ⅝ |
| 8542 | - | - | + | ⅞ |
| 8543 | - | - | + | ⅟ |
| 8544 | - | - | + | Ⅰ |
| 8545 | - | - | + | Ⅱ |
| 8546 | - | - | + | Ⅲ |
| 8547 | - | - | + | Ⅳ |
| 8548 | - | - | + | Ⅴ |
| 8549 | - | - | + | Ⅵ |
| 8550 | - | - | + | Ⅶ |
| 8551 | - | - | + | Ⅷ |
| 8552 | - | - | + | Ⅸ |
| 8553 | - | - | + | Ⅹ |
| 8554 | - | - | + | Ⅺ |
| 8555 | - | - | + | Ⅻ |
| 8556 | - | - | + | Ⅼ |
| 8557 | - | - | + | Ⅽ |
| 8558 | - | - | + | Ⅾ |
| 8559 | - | - | + | Ⅿ |
| 8560 | - | - | + | ⅰ |
| 8561 | - | - | + | ⅱ |
| 8562 | - | - | + | ⅲ |
| 8563 | - | - | + | ⅳ |
| 8564 | - | - | + | ⅴ |
| 8565 | - | - | + | ⅵ |
| 8566 | - | - | + | ⅶ |
| 8567 | - | - | + | ⅷ |
| 8568 | - | - | + | ⅸ |
| 8569 | - | - | + | ⅹ |
| 8570 | - | - | + | ⅺ |
| 8571 | - | - | + | ⅻ |
| 8572 | - | - | + | ⅼ |
| 8573 | - | - | + | ⅽ |
| 8574 | - | - | + | ⅾ |
| 8575 | - | - | + | ⅿ |
| 8576 | - | - | + | ↀ |
| 8577 | - | - | + | ↁ |
| 8578 | - | - | + | ↂ |
| 8581 | - | - | + | ↅ |
| 8582 | - | - | + | ↆ |
| 8583 | - | - | + | ↇ |
| 8584 | - | - | + | ↈ |
| 8585 | - | - | + | ↉ |
| 9312 | + | - | + | ① |
| 9313 | + | - | + | ② |
| 9314 | + | - | + | ③ |
| 9315 | + | - | + | ④ |
| 9316 | + | - | + | ⑤ |
| 9317 | + | - | + | ⑥ |
| 9318 | + | - | + | ⑦ |
| 9319 | + | - | + | ⑧ |
| 9320 | + | - | + | ⑨ |
| 9321 | - | - | + | ⑩ |
| 9322 | - | - | + | ⑪ |
| 9323 | - | - | + | ⑫ |
| 9324 | - | - | + | ⑬ |
| 9325 | - | - | + | ⑭ |
| 9326 | - | - | + | ⑮ |
| 9327 | - | - | + | ⑯ |
| 9328 | - | - | + | ⑰ |
| 9329 | - | - | + | ⑱ |
| 9330 | - | - | + | ⑲ |
| 9331 | - | - | + | ⑳ |
| 9332 | + | - | + | ⑴ |
| 9333 | + | - | + | ⑵ |
| 9334 | + | - | + | ⑶ |
| 9335 | + | - | + | ⑷ |
| 9336 | + | - | + | ⑸ |
| 9337 | + | - | + | ⑹ |
| 9338 | + | - | + | ⑺ |
| 9339 | + | - | + | ⑻ |
| 9340 | + | - | + | ⑼ |
| 9341 | - | - | + | ⑽ |
| 9342 | - | - | + | ⑾ |
| 9343 | - | - | + | ⑿ |
| 9344 | - | - | + | ⒀ |
| 9345 | - | - | + | ⒁ |
| 9346 | - | - | + | ⒂ |
| 9347 | - | - | + | ⒃ |
| 9348 | - | - | + | ⒄ |
| 9349 | - | - | + | ⒅ |
| 9350 | - | - | + | ⒆ |
| 9351 | - | - | + | ⒇ |
| 9352 | + | - | + | ⒈ |
| 9353 | + | - | + | ⒉ |
| 9354 | + | - | + | ⒊ |
| 9355 | + | - | + | ⒋ |
| 9356 | + | - | + | ⒌ |
| 9357 | + | - | + | ⒍ |
| 9358 | + | - | + | ⒎ |
| 9359 | + | - | + | ⒏ |
| 9360 | + | - | + | ⒐ |
| 9361 | - | - | + | ⒑ |
| 9362 | - | - | + | ⒒ |
| 9363 | - | - | + | ⒓ |
| 9364 | - | - | + | ⒔ |
| 9365 | - | - | + | ⒕ |
| 9366 | - | - | + | ⒖ |
| 9367 | - | - | + | ⒗ |
| 9368 | - | - | + | ⒘ |
| 9369 | - | - | + | ⒙ |
| 9370 | - | - | + | ⒚ |
| 9371 | - | - | + | ⒛ |
| 9450 | + | - | + | ⓪ |
| 9451 | - | - | + | ⓫ |
| 9452 | - | - | + | ⓬ |
| 9453 | - | - | + | ⓭ |
| 9454 | - | - | + | ⓮ |
| 9455 | - | - | + | ⓯ |
| 9456 | - | - | + | ⓰ |
| 9457 | - | - | + | ⓱ |
| 9458 | - | - | + | ⓲ |
| 9459 | - | - | + | ⓳ |
| 9460 | - | - | + | ⓴ |
| 9461 | + | - | + | ⓵ |
| 9462 | + | - | + | ⓶ |
| 9463 | + | - | + | ⓷ |
| 9464 | + | - | + | ⓸ |
| 9465 | + | - | + | ⓹ |
| 9466 | + | - | + | ⓺ |
| 9467 | + | - | + | ⓻ |
| 9468 | + | - | + | ⓼ |
| 9469 | + | - | + | ⓽ |
| 9470 | - | - | + | ⓾ |
| 9471 | + | - | + | ⓿ |
| 10102 | + | - | + | ❶ |
| 10103 | + | - | + | ❷ |
| 10104 | + | - | + | ❸ |
| 10105 | + | - | + | ❹ |
| 10106 | + | - | + | ❺ |
| 10107 | + | - | + | ❻ |
| 10108 | + | - | + | ❼ |
| 10109 | + | - | + | ❽ |
| 10110 | + | - | + | ❾ |
| 10111 | - | - | + | ❿ |
| 10112 | + | - | + | ➀ |
| 10113 | + | - | + | ➁ |
| 10114 | + | - | + | ➂ |
| 10115 | + | - | + | ➃ |
| 10116 | + | - | + | ➄ |
| 10117 | + | - | + | ➅ |
| 10118 | + | - | + | ➆ |
| 10119 | + | - | + | ➇ |
| 10120 | + | - | + | ➈ |
| 10121 | - | - | + | ➉ |
| 10122 | + | - | + | ➊ |
| 10123 | + | - | + | ➋ |
| 10124 | + | - | + | ➌ |
| 10125 | + | - | + | ➍ |
| 10126 | + | - | + | ➎ |
| 10127 | + | - | + | ➏ |
| 10128 | + | - | + | ➐ |
| 10129 | + | - | + | ➑ |
| 10130 | + | - | + | ➒ |
| 10131 | - | - | + | ➓ |
PS: I am sorry for the wrong format.