当前位置:科普知识站>IT科技>

python中endswith

IT科技 阅读(2.65W)

endswith是属于python下的一个方法,它能够用来判断字符串是否以指定后缀结尾,若是以指定后缀结尾返回True,否则是返回False,其中可以选择参数start与end为检索字符串的开始与结束位置。

endswith()方法的语法格式为:

str.endswith(suffix[, start[, end]])

python中endswith

参数说明:

suffix --该参数能够是一个字符串或是一个元素。

start -- 字符串中的开始位置。

end -- 字符中结束位置。

返回值:

若是字符串中含有指定的后缀则返回True,否则返回False。

python中endswith 第2张

参考范例:

endswith()方法的使用,具体代码为:

#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";

print str.endswith(suffix);

print str.endswith(suffix,20);

suffix = "is";

print str.endswith(suffix, 2, 4);

print str.endswith(suffix, 2, 6);

输出结果:

True

True

True

False