str.isdecimal()和str.isdigit()差异examp

2024-06-10 11:49:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在阅读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

Tags: 函数字符串代码文档falsetrue示例文献
3条回答

让我们看一些例子:

str.isdecimal() (Only Decimal Numbers)

34是十进制数吗?-->;是

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

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

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

str.isdigit() (Decimals, Subscripts, Superscripts)

34是数字吗?-->;是

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

上标2是数字吗?-->;是

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

str.isnumeric() (Decimals, Subscripts, Superscripts, Vulgar Fractions, Roman Numerals, Currency Numerators)

34是数字吗?-->;是

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

上标2是数字吗?-->;是

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

普通分数是四分之一的数字吗?-->;是

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

如果你怀疑,我的建议是——编码,看结果,得出结论。

密码

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。

差异,但有点罕见。它主要使用各种unicode字符,如2

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

您还可以使用isnumeric方法进一步深入细致的unicode区分兔子洞:

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

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

相关问题 更多 >