Python中startswith和endswith的用法
程序员文章站
2022-07-16 14:27:44
...
描述
Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数”start”与”end”为检索字符串的开始与结束位置。
语法
endswith()方法语法:
str.endswith(suffix[, start[, end]])
参数
suffix – 该参数可以是一个字符串或者是一个元素。
start – 字符串中的开始位置。
end – 字符中结束位置。
返回值
如果字符串含有指定的后缀返回True,否则返回False。
实例
以下实例展示了endswith()方法的实例:
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
推荐阅读
-
详解Python中open()函数指定文件打开方式的用法
-
从源码解析Python的Flask框架中request对象的用法
-
对python模块中多个类的用法详解
-
python中pycurl库的用法实例
-
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
-
Python中的异常处理try/except/finally/raise用法分析
-
使用Python和xlwt向Excel文件中写入中文的实例
-
Python中bisect的用法
-
Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)
-
详谈Python中列表list,元祖tuple和numpy中的array区别