当前位置:  首页>> 技术小册>> Python合辑3-字符串用法深度总结

str.endswith(suffix) 返回 True 是以指定后缀结尾的字符串。如果字符串以指定的前缀开头,str.startswith(prefix) 返回 True:

  1. sentences = ['Time to master data science', 'I love statistical computing', 'Eat, sleep, code']
  2. # endswith() example
  3. for one_sentence in sentences:
  4. print(one_sentence.endswith(('science', 'computing', 'Code')))

Output:

  1. True
  2. True
  3. False

startswith() example

  1. for one_sentence in sentences:
  2. print(one_sentence.startswith(('Time', 'I ', 'Ea')))

Output:

  1. True
  2. True
  3. True

该分类下的相关小册推荐: