如何判断一个字符串是数字


本文摘自php中文网,作者anonymity,侵删。

Python中字符串处理的方法函数:

str.isnumeric(): True if 只包含数字;otherwise False。注意:此函数只能用于unicode string

str.isdigit(): True if 只包含数字;otherwise False。

str.isalpha():True if 只包含字母;otherwise False。

str.isalnum():True if 只包含字母或者数字;otherwise False。

示例字符串:

str_1 = "123"

str_2 = "Abc"

str_3 = "123Abc"

代码处理过程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#用isdigit函数判断是否数字

print(str_1.isdigit())

Ture

print(str_2.isdigit())

False

print(str_3.isdigit())

False

#用isalpha判断是否字母

print(str_1.isalpha())   

False

print(str_2.isalpha())

Ture   

print(str_3.isalpha())   

False

#isalnum判断是否数字和字母的组合

print(str_1.isalnum())   

Ture

print(str_2.isalnum())

Ture

print(str_1.isalnum())   

Ture

注意:如果字符串中含有除了字母或者数字之外的字符,比如空格,也会返回False

严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会False
isalnum()必须是数字和字母的混合
isalpha()不区分大小写

以上就是如何判断一个字符串是数字的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python怎么读取图片

Python中数据结构与算法的应用(附示例)

Python中dateutil模块的理解(附示例)

Python用什么解释器?

Python怎么调用idle

Python学什么方向

Python数据分析用什么数据库

Python教程之字符串

传授 每30秒学会一个Python小技巧

Python生成不重复随机数和对list乱序的解决方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...