Python 中判断字符串是否只包含数字的三个方法比较:isdecimal、isdigit 和 isnumeric

Python 中判断字符串是否只包含数字的三个方法比较:isdecimal、isdigit 和 isnumeric

首先,在Python 中对于 字符串 Sting 类型来说,存在很多的方法,如下:

在 ipython 中定义一个 字符串,例如:hello_str = ""

输入 hello_str. 按下 TAB 键,ipython 会提示 字符串 能够使用的 方法 如下:

In [1]: hello_str.
hello_str.capitalize    hello_str.isidentifier  hello_str.rindex
hello_str.casefold      hello_str.islower       hello_str.rjust
hello_str.center        hello_str.isnumeric     hello_str.rpartition
hello_str.count         hello_str.isprintable   hello_str.rsplit
hello_str.encode        hello_str.isspace       hello_str.rstrip
hello_str.endswith      hello_str.istitle       hello_str.split
hello_str.expandtabs    hello_str.isupper       hello_str.splitlines
hello_str.find          hello_str.join          hello_str.startswith
hello_str.format        hello_str.ljust         hello_str.strip
hello_str.format_map    hello_str.lower         hello_str.swapcase
hello_str.index         hello_str.lstrip        hello_str.title
hello_str.isalnum       hello_str.maketrans     hello_str.translate
hello_str.isalpha       hello_str.partition     hello_str.upper
hello_str.isdecimal     hello_str.replace       hello_str.zfill
hello_str.isdigit       hello_str.rfind

在这篇文章中,我们只讨论其中的三个方法:

string.isdecimal()、string.isdigit() 和 string.isnumeric()

咋一看,这三个方法都是判断字符串是否只包含数字,但是其实它们还是有很大的区别的:

string.isdecimal()如果 string 只包含数字则返回 True,全角数字
string.isdigit()如果 string 只包含数字则返回 True,全角数字\u00b2
string.isnumeric()如果 string 只包含数字则返回 True,全角数字汉字数字

1、都能判断 阿拉伯数字:

# 判断字符串是否只包含数字
num_str = "1"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
1
True
True
True

2、都不能判断小数:

# 判断字符串是否只包含数字
num_str = "1.1"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
1.1
False
False
False

3、与 isdecimal() 相比,isdigit() 和 isnumeric() 都能判断 unicode 编码的数字字符串:如    、①和 \u00b2

# 判断字符串是否只包含数字
num_str = "\u00b2"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())

num_str_1 = "⑴"
print(num_str_1)
print(num_str_1.isdecimal())
print(num_str_1.isdigit())
print(num_str_1.isnumeric())

num_str_2 = "①"
print(num_str_2)
print(num_str_2.isdecimal())
print(num_str_2.isdigit())
print(num_str_2.isnumeric())
²
False
True
True
⑴
False
True
True
①
False
True
True

4、与 isdigit() 相比, isnumeric() 还能判断 中文数字:如 一千一百零一

# 判断字符串是否只包含数字
num_str = "一千一百零一"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
一千一百零一
False
False
True
# 判断字符串是否只包含数字
num_str = "壹贰叁肆伍陆柒捌玖拾佰仟萬亿"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
壹贰叁肆伍陆柒捌玖拾佰仟萬亿
False
False
True

总结:

简单一句话来说,对于数字的定义来说,广义度:isnumeric() >  isdigit() > isdecimal()。

在 开发中最常用的还是仅判断阿拉伯数字的 isdecimal() 方法。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值