python isdigit和isnumeric_python字符串isdigit、isnumeric、isdecimal区别

isdigit()、isnumeric()、isdecimal()是python语言中字符串的内置类型。这三个函数主要区别是由于Unicode类型产生的。

decimal字符举例:

"12345"

"12"

"98201"

digits字符举例:

"12345"

"1233⁴"

"⁴"

numerics字符举例:

"12345"

"½¼"

"½"

"12345½"

代码示例:

str1 = "362436" #decimal characters

str2 = "3" #unicode digit

str3 = "½¼" #fractional value

print("str1 :", str1)

print("str1.isdecimal () : ", str1.isdecimal ())

print("str1.isnumeric () : ", str1.isnumeric ())

print("str1.isdigit () : ", str1.isdigit ())

print("str2 :", str2)

print("str2.isdecimal () : ", str2.isdecimal ())

print("str2.isnumeric () : ", str2.isnumeric ())

print("str2.isdigit () : ", str2.isdigit ())

print("str3 :", str3)

print("str3.isdecimal () : ", str3.isdecimal ())

print("str3.isnumeric () : ", str3.isnumeric ())

print("str3.isdigit () : ", str3.isdigit ())

运行结果:

str1 : 362436

str1.isdecimal () : True

str1.isnumeric () : True

str1.isdigit () : True

str2 : 3

str2.isdecimal () : True

str2.isnumeric () : True

str2.isdigit () : True

str3 : ½¼

str3.isdecimal () : False

str3.isnumeric () : True

str3.isdigit () : False

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值