isdigit函数python检测数字个数_Python中判断字符串是否为数字的三个方法isdecimal 、isdigit、isnumeric的差别...
isdecimal 、isdigit、isnumeric這三個字符串方法都用于判斷字符串是否為數(shù)字,為什么用三個方法呢?他們的差別是什么內?
isdecimal:是否為十進制數(shù)字符,包括Unicode數(shù)字、雙字節(jié)全角數(shù)字,不包括羅馬數(shù)字、漢字數(shù)字、小數(shù);
isdigit:是否為數(shù)字字符,包括Unicode數(shù)字,單字節(jié)數(shù)字,雙字節(jié)全角數(shù)字,不包括漢字數(shù)字,羅馬數(shù)字、小數(shù)
isnumeric:是否所有字符均為數(shù)值字符,包括Unicode數(shù)字、雙字節(jié)全角數(shù)字、羅馬數(shù)字、漢字數(shù)字,不包括小數(shù)。
我們定義一個函數(shù)來進行驗證:
def isnumber(s):
print(s+' isdigit: ',s.isdigit())
print(s+' isdecimal: ',s.isdecimal())
print(s+' isnumeric: ',s.isnumeric())
執(zhí)行函數(shù)isnumber(‘123’),三個都是True,執(zhí)行函數(shù)isnumber(‘123.0’),三個都是False,執(zhí)行函數(shù)isnumber(‘壹貳叁’),打印分別是False、False、True,羅馬數(shù)字也是一樣的結果。
>>> isnumber('123')
123 isdigit: True
123 isdecimal: True
123 isnumeric: True
>>> isnumber('123.0')
123.0 isdigit: False
123.0 isdecimal: False
123.0 isnumeric: False
>>> isnumber('壹貳叁')
壹貳叁 isdigit: False
壹貳叁 isdecimal: False
壹貳叁 isnumeric: True
總結
以上是生活随笔為你收集整理的isdigit函数python检测数字个数_Python中判断字符串是否为数字的三个方法isdecimal 、isdigit、isnumeric的差别...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python sanic orm_San
- 下一篇: python深度神经网络量化_深度神经网