导语:
本文主要介绍了关于Python endswith()方法的相关知识,包括Python lambda,以及python调用类方法这些编程知识,希望对大家有参考作用。
描述
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 = "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'))
以上实例输出结果如下:
True
True
True
False
-----------------------------
True
True
False
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何压缩python列表12/06
- ♥ Python3中def的使用08/23
- ♥ 如何退出python命令行09/01
- ♥ python如何使用实例作为属性11/20
- ♥ 如何在 Python 中拆分多级嵌套列表11/18
- ♥ 如何设置环境变量10/28
内容反馈