Python endswith()方法
描述
Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。
语法
endswith()方法语法:
<pre class="brush:html;toolbar:false">string.endswith(suffix[, start[, end]]) # 自Python2.5版本起,还支持接收一个 tuple 为参数 string.endswith(tuple) # 满足tuple任何一个都返回True</pre>参数
suffix -- 该参数可以是一个字符串或者是一个元素This could be a string or could also be a tuple of suffixes to look for.
start -- 字符串中的开始位置。
end -- 字符中结束位置。
返回值
如果字符串含有指定的后缀返回True,否则返回False。
实例
以下实例展示了endswith()方法的实例:
<pre class="brush:html;toolbar:false">#!/usr/bin/python string = "this is string example....wow!!!"; suffix = "wow!!!"; print string.endswith(suffix); print string.endswith(suffix,20); suffix = "is"; print string.endswith(suffix, 2, 4); print string.endswith(suffix, 2, 6); print '-----------------------------' string1 = 'abc' string2 = 'xyz' string3 = 'twz' for string in [string1,string2,string3]: print string.endswith(('a','x'))</pre>以上实例输出结果如下:
<pre class="brush:html;toolbar:false">True True True False ----------------------------- True True False</pre><span style="color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; white-space: normal; background-color: rgb(255, 255, 255);">
</span>
您可能感兴趣的文章:
Python endswith()方法
如何用python去保存文件后缀名
Python startswith()和endswith()方法
python如何判断字符串以什么结尾
Python字符串操作常用知识点(3)
Python dir()和help()帮助函数
python如何查看类的函数
Python字符串操查找替换分割和连接方的法及使用
Python字符串大小写转换的函数及用法
Asp.Net 上传图片并生成高清晰缩略图