Python字符串(三):字符串的判断

1.字符串的判断

isalnum判断字符串是否完全由字母或数字组成
isalpha判断字符串是否完全由字母组成
isdigit判断字符串是否完全由数字组成
isupper判断字符串当中的字母是否完全是大写
islower判断字符串中的字母是否完全是小写
istitle判断字符串是否满足title格式
isspace判断字符串是否完全由空格组成
startswith判断字符串的开头字符,也可以截取判断
endswith判断字符串的结尾字符,也可以截取判断
split判断字符串的分隔符切片

练习:isalnum()

print('hello123'.isalnum())
print('hello123@'.isalnum())
运行结果:True
        False

练习:isalpha()

print('hello12'.isalpha())
print('hello'.isalpha())
运行结果:False
        True

练习:isdigit()

print('hello12'.isdigit())
print('123'.isdigit())
运行结果:False
        True

练习:isupper()

print('HE'.isupper())
print('hL'.isupper())
运行结果:True
        False

练习:isspace()

print(' '.isspace())
print('h'.isspace())
运行结果:True
        False

练习:startswith()、endswith()

print('hello'.startswith('h'))               #判断是否以‘h’开头
print('hello'.startswith('l'))
print('hello'.endswith('h'))                 #判断是否以‘h’结尾
print('hello'.endswith('o'))
运行结果:True
        False
        False
        True

练习:split()

print('hello world'.split())
print('hello'.split())
print('hello'.split('h'))
运行结果:
['hello', 'world']
['hello']
['', 'ello']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值