isdigit函数python检测数字个数_Python isdecimal isdigit isnumeric区别
Python isdecimal() isdigit() isnumeric()區別教程
isdecimal() isdigit() isnumeric()比較
函數
描述
isdecimal()
是否為十進制數字符,包括 Unicode 數字、雙字節全角數字,不包括羅馬數字、漢字數字、小數。
isdigit()
是否為數字字符,包括 Unicode 數字,單字節數字,雙字節全角數字,不包括漢字數字,羅馬數字、小數。
isnumeric()
是否所有字符均為數值字符,包括 Unicode 數字、雙字節全角數字、羅馬數字、漢字數字,不包括小數。
案例
純數字
使用純數字,比較isdecimal() isdigit() isnumeric()區別
print("嗨客網(www.haicoder.net)")
# 使用純數字,比較isdecimal() isdigit() isnumeric()區別
def isnumber(s):
print(s+' isdigit: ', s.isdigit())
print(s+' isdecimal: ', s.isdecimal())
print(s+' isnumeric: ', s.isnumeric())
print(isnumber('123'))
程序運行后,控制臺輸出如下:
首先,我們定義一個函數 isnumber() 傳入一個字符串參數,最終打印該字符串
我們傳入一個純數字的字符串,最終三個函數都返回
帶小數的數字字符串
使用帶小數的數字字符串,比較isdecimal() isdigit() isnumeric()區別
print("嗨客網(www.haicoder.net)")
# 使用帶小數的數字字符串,比較isdecimal() isdigit() isnumeric()區別
def isnumber(s):
print(s+' isdigit: ', s.isdigit())
print(s+' isdecimal: ', s.isdecimal())
print(s+' isnumeric: ', s.isnumeric())
print(isnumber('123.1'))
程序運行后,控制臺輸出如下:
首先,我們定義一個函數 isnumber() 傳入一個字符串參數,最終打印該字符串變量的 isdigit()、isdecimal() 和 isnumeric() 的運行結果。
我們傳入一個帶小數的數字字符串,最終三個函數都返回 False,即帶小數的數字字符串使用這三個函數返回的結果是一致的。
大寫中文數字字符串
使用大寫中文數字字符串,比較isdecimal() isdigit() isnumeric()區別
print("嗨客網(www.haicoder.net)")
# 使用大寫中文數字字符串,比較isdecimal() isdigit() isnumeric()區別
def isnumber(s):
print(s+' isdigit: ', s.isdigit())
print(s+' isdecimal: ', s.isdecimal())
print(s+' isnumeric: ', s.isnumeric())
print(isnumber('壹貳叁'))
程序運行后,控制臺輸出如下:
首先,我們定義一個函數 isnumber() 傳入一個字符串參數,最終打印該字符串變量的 isdigit()、isdecimal() 和 isnumeric() 的運行結果。
我們傳入一個大寫中文數字字符串,最終只有 isnumeric() 返回了 True。
Python isdecimal isdigit isnumeric區別總結
Python 判斷字符串是否是數字主要有三種方法,分別為 isnumeric() 、isdigit() 和 isdecimal()。
isdecimal() :是否為十進制數字符,包括 Unicode 數字、雙字節全角數字,不包括羅馬數字、漢字數字、小數。
isdigit() :是否為數字字符,包括Unicode數字,單字節數字,雙字節全角數字,不包括漢字數字,羅馬數字、小數。
isnumeric() :是否所有字符均為數值字符,包括Unicode數字、雙字節全角數字、羅馬數字、漢字數字,不包括小數。
總結
以上是生活随笔為你收集整理的isdigit函数python检测数字个数_Python isdecimal isdigit isnumeric区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php如何取消bak自动备份_教你如何进
- 下一篇: c语言调用python变量_Python