判断大小写字母、数字、标题
>>> 'Hello'.istitle()
True
>>> 'hello'.istitle()
False
>>> 'hello'.isupper()
False
>>> 'hello'.islower()
True
>>> 'HHHello'.islower()
False
>>> 'HHHello'.isupper()
False
>>> 'HELLO'.lower()
'hello'
>>> a = 'HELLO'.lower()
>>> a
'hello'
>>> 'Herwr'.upper()
'HERWR'
>>> 'HELLO'.title()
'Hello'
print('Hello'.istitle())
print('hello'.istitle())
print('hello'.isupper())
print('HHHello'.isupper())
print('hello'.islower())
print('HHHello'.islower())
print('HELLO'.lower())
print('123'.isalpha())
print('sdff'.isalpha())
结果
判断文件名称的结尾
filename = 'hello.loggggg'
if filename.endswith('.log'):
print(filename)
else:
print('error.file')
结果
判断文件名称的开头
url = 'https://172.25.254.250/index.html'
if url.startswith('http://'):
print('爬取网页')
else:
print('不能爬取')
结果
判断
[[:digit:]] [[:alpha:]]
# 只要有一个元素不满足条件 就返回False
print('3121asdas'.isdigit())
print('dsaddada'.isalpha())
print('dasdaad442134'.isalnum())
结果