python中str函数isdigit、isdecimal、isnumeric的区别

时间:2025-01-20 07:00:42
num = "1" #unicode () # True () # True () # True num = "1" # 全角 () # True () # True () # True num = b"1" # byte () # True () # AttributeError 'bytes' object has no attribute 'isdecimal' () # AttributeError 'bytes' object has no attribute 'isnumeric' num = "IV" # 罗马数字 () # True () # False () # True num = "四" # 汉字 () # False () # False () # True =================== isdigit() True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字 False: 汉字数字 Error: 无 isdecimal() True: Unicode数字,,全角数字(双字节) False: 罗马数字,汉字数字 Error: byte数字(单字节) isnumeric() True: Unicode数字,全角数字(双字节),罗马数字,汉字数字 False: 无 Error: byte数字(单字节) ================ import unicodedata ("2") # 2 ("2") # 2 ("2") # 2.0 ("2") # 2 ("2") # 2 ("2") # 2.0 (b"3") # TypeError: must be str, not bytes (b"3") # TypeError: must be str, not bytes (b"3") # TypeError: must be str, not bytes ("Ⅷ") # ValueError: not a digit ("Ⅷ") # ValueError: not a decimal ("Ⅷ") # 8.0 ("四") # ValueError: not a digit ("四") # ValueError: not a decimal ("四") # 4.0 #"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿"