Python str.isdecimal() 和 str.isdigit() 区别示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22789392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:41:16  来源:igfitidea点击:

str.isdecimal() and str.isdigit() difference example

pythonpython-3.x

提问by Phoenix

Reading python docs I have come to .isdecimal() and .isdigit() string functions and i'm not finding literature too clear on their usable distinction. Could someone supply me with code examples of where these two functions differentiate please.

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

Similar behaviour:

类似行为:

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

采纳答案by Henry Keiter

There aredifferences, but they're somewhat rare*. It mainly crops up with various unicode characters, such as 2:

差异,但他们有些罕见*。它主要包含各种 unicode 字符,例如2

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

You can also go further down the careful-unicode-distinction rabbit hole with the isnumericmethod:

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

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

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

回答by N Randhawa

Lets see some examples:

让我们看一些例子:

str.isdecimal()(Only Decimal Numbers)

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

Is 34 a decimal number? --> Yes

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

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

Is superscript 2 a decimal number? --> No

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

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

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

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

Is 34 a digit? --> Yes

34是数字吗?--> 是的

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

Is superscript 2 a digit? --> Yes

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

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

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

str.isnumeric()(小数、下标、上标、通俗分数、罗马数字、货币分子)

Is 34 a numeric number? --> Yes

34是数字吗?--> 是的

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

Is superscript 2 a numeric number? --> Yes

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

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

Is Vulgar Fraction one Quarter numeric number? -->Yes

粗俗分数是四分之一数字吗?-->是的

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

回答by PADYMKO

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 |    +    |     -     | 2   
|    179 |    +    |     -     | 3   
|    185 |    +    |     -     | 1   
|   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.

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