Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。
str.endswith(suffix[, start[, end]])
str = "this is string example....wow!!!"; #这是区分大小写的,如果是Wow!!!则是False suffix = "wow!!!" print (str.endswith(suffix)) #True print (str.endswith(suffix,20)) #True # 这里不用从20开始,从10也是True,因为wow是在它们之后才出现 suffix = "is"; print (str.endswith(suffix, 2, 4)) #True print (str.endswith(suffix, 2, 6)) #False
对上面的解释做一个补充,endswith 里面的 start 表示搜索指定 str/list 等的开始位置,默认是 0,即从第一个元素开始搜索,end 默认是字符串/列表等的长度,表示结束搜索的位置
原文:https://www.cnblogs.com/cgmcoding/p/14429011.html