当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

检查字符串是否以列表中的一个字符串结尾

  1. str_list = ['aaa', 'bbb', 'ccc', 'ddd'] # list of items
  2. str_test = 'testccc' # string need to test
  3. for str_item in str_list:
  4. if str_test.endswith(str_item):
  5. print("found")
  6. break # loop ends when result found
  7. else:
  8. print("not found")

Output:

  1. not found
  2. not found
  3. found