- startsWith的使用
- endsWith的使用
import os
filenames = os.listdir('.')
filename = 'spam.txt'
print(filename.endswith('.txt'))
print(filename.startswith('file:'))
url = 'http://www.python.org'
print(url.startswith('http:'))
print(filenames)
print([name for name in filenames if name.endswith('.py')])
print(any(name.endswith('.py') for name in filenames))
choices = ['http:', 'ftp:']
url = 'http://python.org'
url.startswith(tuple(choices))
True
False
True
['ch_02_01.py', 'ch_02_02.py', 'ch_02_03.py', 'ch_02_04.py', 'ch_02_05.py', 'ch_
02_06.py', 'ch_02_07.py', 'ch_02_08.py', 'ch_02_09.py', 'ch_02_10.py', 'ch_02_11
.py', 'ch_02_13.py', 'ch_02_14.py', 'ch_02_15.py', 'ch_02_16.py', 'ch_02_18.py',
'ch_02_20.py']
['ch_02_01.py', 'ch_02_02.py', 'ch_02_03.py', 'ch_02_04.py', 'ch_02_05.py', 'ch_
02_06.py', 'ch_02_07.py', 'ch_02_08.py', 'ch_02_09.py', 'ch_02_10.py', 'ch_02_11
.py', 'ch_02_13.py', 'ch_02_14.py', 'ch_02_15.py', 'ch_02_16.py', 'ch_02_18.py',
'ch_02_20.py']
True