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']