当前位置: 首页 > 图灵资讯 > 行业资讯> Python endswith()方法

Python endswith()方法

来源:图灵python
时间: 2025-10-15 21:17:45

描述

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

语法

endswith()方法语法:

string.endswith(suffix[,start[,end]])
#自Python2.5版本以来,它还支持以参数接收tuple
string.endswith(tuple)#任何满足tuple的人都会返回true

参数

suffix -- 参数可以是字符串或元素This could be a string or could also be a tuple of suffixes to look for.

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

end -- 结束位置在字符中。

返回值

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

实例

以下实例展示了endswith()方法的实例:

#!/usr/bin/python

string="thisisstringexample...wow!!!";

suffix="wow!!!";
printstring.endswith(suffix);
printstring.endswith(suffix,20);

suffix="is";
printstring.endswith(suffix,2,4);
printstring.endswith(suffix,2,6);

print'-----------------------------'

string1='abc'
string2='xyz'
string3='twz'
forstringin[string1,string2,string3]:
printstring.endswith(('a','x'))

上述实例的输出结果如下:

True
True
True
False
-----------------------------
True
True
False