Python string 的 endswith()方法

 

Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

str.endswith(suffix[, start[, end]])
  • suffix -- 该参数可以是一个字符串或者是一个元素。
  • start -- 字符串中的开始位置。
  • end -- 字符中结束位置。
  • 如果字符串含有指定的后缀返回True,否则返回False。
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 默认是字符串/列表等的长度,表示结束搜索的位置

 

posted on 2021-02-22 11:03  小小喽啰  阅读(560)  评论(0编辑  收藏  举报