python之判断字符串中是否包含数字

1. “isdigit”  函数,判断字符串中是否全部为“数字”。 如果字符串中有一个不是数字, 则为False

strs = "123"

if strs.isdigit():
    print("是数字")
else:
    print("不是数字")

运行结果:

>>>是数字

 

2. 判断字符串中是否包含“数字”, 使用for循环, 结合 isdigit  就可以办到

strs = "中123文"

for s in strs:
    if s.isdigit():
        print("包含数字")
        break
else:
    print("不包含数字")

运行结果:

包含数字

 

3. 其它字符串判断函数。

str为字符串
str.isalnum() 所有字符都是数字或者字母
str.isalpha() 所有字符都是字母
str.isdigit() 所有字符都是数字
str.islower() 所有字符都是小写
str.isupper() 所有字符都是大写
str.istitle() 所有单词都是首字母大写,像标题
str.isspace() 所有字符都是空白字符、\t、\n、\r

 

posted @ 2022-03-09 18:29  博无止境  阅读(10707)  评论(0编辑  收藏  举报