python-10.菜鳥教程-6-str.isdecimal () 與str.isdigit()的區別

Python3 isnumeric()方法

Python3 字符串 Python3 字符串

描述

isnumeric() 方法檢測字符串是否只由數字組成,數字可以是: Unicode 數字,全角數字(雙字節),羅馬數字,漢字數字。

指數類似 ² 與分數類似 ½ 也屬於數字。

# s = '½'
s = '\u00BD'

語法

isnumeric()方法語法:

str.isnumeric()

參數

  • 無。

返回值

如果字符串中只包含數字字符,則返回 True,否則返回 False

實例

以下實例展示了 isnumeric() 方法的實例:

#!/usr/bin/python3

str = "runoob2016"  
print (str.isnumeric())

str = "23443434"
print (str.isnumeric())

以上實例輸出結果如下:

False
True

Unicode 數字:

#!/usr/bin/python3

# s = '²3455'
s = '\u00B23455'
print(s.isnumeric())
# s = '½'
s = '\u00BD'
print(s.isnumeric())

a = "\u0030"  # unicode for 0
print(a.isnumeric())

b = "\u00B2"  # unicode for ²
print(b.isnumeric())

c = "10km2"
print(c.isnumeric())

以上實例輸出結果如下:

True
True
True
True
False

MEMO

str.isdecimal () 與str.isdigit()的區別

str.isdecimal() 函數只對十進制數返回 True,同時函數 str.isdigit() 對其他 unicode 支持的字符返回 True。

詳細的內容可以使用以下代碼輸出:

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

s.isdigit、isdecimal 和 s.isnumeric 區別

isdigit()

True: Unicode數字,byte數字(單字節),全角數字(雙字節)

False: 漢字數字,羅馬數字,小數

Error: 無

isdecimal()

True: Unicode數字,,全角數字(雙字節)

False: 羅馬數字,漢字數字,小數

Error: byte數字(單字節)

isnumeric()

True: Unicode 數字,全角數字(雙字節),漢字數字

False: 小數,羅馬數字

Error: byte數字(單字節)

num = "1"  # unicode
num.isdigit()       # True
num.isdecimal()     # True
num.isnumeric()     # True

num = "1"           # 全角
num.isdigit()       # True
num.isdecimal()     # True
num.isnumeric()     # True

num = b"1"          # byte
num.isdigit()       # True
num.isdecimal()     # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric()     # AttributeError 'bytes' object has no attribute 'isnumeric'

num = "IV"          # 羅馬數字
num.isdigit()       # False
num.isdecimal()     # False
num.isnumeric()     # False

num = "四"          # 漢字
num.isdigit()       # False
num.isdecimal()     # False
num.isnumeric()     # True
num = "1"           # unicode
num.isdigit()       # True
num.isdecimal()     # True
num.isnumeric()     # True
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())

num = "1"           # 全角
num.isdigit()       # True
num.isdecimal()     # True
num.isnumeric()     # True
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())

num = b"1"          # byte
num.isdigit()       # True
print(num.isdigit())

num = "IV"          # 羅馬數字
num.isdigit()       # False
num.isdecimal()     # False
num.isnumeric()     # False
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())

num = "四"          # 漢字
num.isdigit()       # False
num.isdecimal()     # False
num.isnumeric()     # True
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章