str.isdecimal()和str.isdigit()差异示例

时间:2014-04-01 14:48:48

标签: python python-3.x

阅读python文档我已经找到了.isdecimal()和.isdigit()字符串函数,而且我没有找到关于它们可用区别的文献。有人可以向我提供这两个功能区别的代码示例。

类似的行为:

>>> str.isdecimal('1')
True
>>> str.isdigit('1')
True

>>> str.isdecimal('1.0')
False
>>> str.isdigit('1.0')
False

>>> str.isdecimal('1/2')
False
>>> str.isdigit('1/2')
False

4 个答案:

答案 0 :(得分:27)

存在差异,但它们有点罕见 * 。它主要用于各种unicode字符,例如 2

>>> c = '\u00B2'
>>> c.isdecimal()
False
>>> c.isdigit()
True

您还可以使用isnumeric方法进一步了解仔细的unicode区别兔洞:

>>> c = '\u00BD' # ½
>>> c.isdecimal()
False
>>> c.isdigit()
False
>>> c.isnumeric()
True

* 至少,我从来没有遇到需要区分包含不同类型的这些异常情况的字符串的生产代码,但肯定存在用例。

答案 1 :(得分:13)

让我们看一些例子:

  

str.isdecimal()(仅限十进制数字)

34是十进制数吗? - >是

print("34".isdecimal())  #True

上标2是十进制数吗? - >否

print("\u00B2")
print("\u00B2".isdecimal())  #False
  

str.isdigit()(小数,下标,上标)

34是一个数字吗? - >是

print("34".isdigit()) #True

上标2是一个数字吗? - >是

print("\u00B2")
print("\u00B2".isdigit()) #True
  

str.isnumeric()(数字,粗俗分数,下标,上标,罗马数字,货币分子)

34是数字吗? - >是

print("34".isnumeric()) #True

上标2是数字吗? - >是

print("\u00B2")
print("\u00B2".isnumeric()) #True

Vulgar Fraction是四分之一的数字吗? - >是

print("\u00BC")
print("\u00BC".isnumeric()) #True

答案 2 :(得分:7)

如果您怀疑,我的建议 - 编码,查看结果,得出结论。

代码

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
     ...:         )
     ...:     )
     ...: 

查看结果

-------------------------------------
|    №   | 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 |    +    |     -     | ➒

得出结论

正如您所看到的,函数 str.isdecimal() str.isdigit()之间的主要区别在于:函数 str.isdecimal( )仅对0到9之间的数字返回True,同时函数 str.isdigit()对其他一些支持unicode的字符返回True。

答案 3 :(得分:2)

完成PADYMKO的回答,我们可以添加其他感兴趣的函数并扩展代码

代码

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
        )
    )

结果

-------------------------------------------------
|    №   | 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:我很抱歉格式错误。