str.endswith(suffix) 返回 True 是以指定后缀结尾的字符串。如果字符串以指定的前缀开头,str.startswith(prefix) 返回 True:
sentences = ['Time to master data science', 'I love statistical computing', 'Eat, sleep, code']
# endswith() example
for one_sentence in sentences:
print(one_sentence.endswith(('science', 'computing', 'Code')))
Output:
True
True
False
for one_sentence in sentences:
print(one_sentence.startswith(('Time', 'I ', 'Ea')))
Output:
True
True
True