Python | 字符串isdecimal(),isdigit(),isnumeric()和Methods之间的区别

The methods isdigit(), isnumeric() and isdecimal() are in-built methods of String in python programming language, which are worked with strings as Unicode objects. These functions return either true or false.

方法isdigit() , isnumeric()和isdecimal()是python编程语言中String的内置方法,可将字符串作为Unicode对象使用。 这些函数返回true或false 。

The comparison is based on Unicode classifications,

比较是基于Unicode分类,

isdecimal()isdigit()isnumeric()
Example of string with decimal characters:
"12345"
"12"
"98201"
Example of string with digits:
"12345"
"1233"
"3"
Example of string with numerics:
"12345"
"½¼"
"½"
"12345½"
Returns ‘true’ if all characters of the string are decimal.Returns ‘true’ if all characters of the string are digits.Returns ‘true if all characters of the string are numeric.
Read more:
String isdecimal() Method
Read more:
String isdigit() Method
Read more:
String isnumeric() Method
isdecimal() isdigit() isnumeric()
带十进制字符的字符串示例:
“ 12345”
“ 12”
“ 98201”
带数字的字符串示例:
“ 12345”
“ 123 3
3
带数字的字符串示例:
“ 12345”
“½¼”
“½”
“12345½”
如果字符串的所有字符均为十进制,则返回“ true”。 如果字符串的所有字符都是数字,则返回“ true”。 如果字符串的所有字符均为数字,则返回'true。
阅读更多:
字符串isdecimal()方法
阅读更多:
字符串isdigit()方法
阅读更多:
字符串isnumeric()方法

Example:

例:

str1 = u"362436"    #decimal characters
str2 = u"3"         #unicode digit
str3 = u"½¼"        #fractional value

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

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

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

Output

输出量

    str1 :
    str1.isdecimal () :  True
    str1.isnumeric () :  True
    str1.isdigit () :  True

    str2 :
    str2.isdecimal () :  True
    str2.isnumeric () :  True
    str2.isdigit () :  True

    str3 :
    str3.isdecimal () :  False
    str3.isnumeric () :  True
    str3.isdigit () :  False


翻译自: https://www.includehelp.com/python/difference-between-string-isdecimal-isdigit-isnumeric-and-methods.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值