python endwith_Python endswith()方法 - Python 教程 - 自强学堂

Python endswith()方法

描述

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值