str.endswith
功能描述
如果字符串以指定的后缀结尾则返回True;否则返回False。可选项:限制给定索引内的字符。
语法
str.endswith(suffix[, start[, end]])
参数
- suffix:可以是一个字符串或后缀的tuple元组
- start:查找索引起始位置
- end:查找索引结束位置
返回值
True:以指定字符结尾,否则False
示例
str = "this is string example....wow!!!"
suffix = "wow!!!"
# default start=0,end=len(str)
print(str.endswith(suffix))
# start=20
print(str.endswith(suffix,20))
suffix = "is"
# start = 0, end =4,对应is
print(str.endswith(suffix, 2, 4))
# start = 0, end = 6,对应is i
# 注意左包括,右不包括,因此[2,6]实际对应的是str[2]~str[5]
print(str.endswith(suffix, 2, 6))
执行结果
True
True
True
False