首页 > 编程语言 > 详细

Python string 的 endswith()方法

时间:2021-02-22 12:08:02      阅读:30      评论:0      收藏:0      [点我收藏+]

 

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 默认是字符串/列表等的长度,表示结束搜索的位置

 

Python string 的 endswith()方法

原文:https://www.cnblogs.com/cgmcoding/p/14429011.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!