在Python中操作字符串之startswith()方法的使用

WBOY
Release: 2016-06-10 15:12:13
Original
1841 people have browsed it

 startswith()方法检查字符串是否以str开始,任选限制匹配与给定索引的开始和结束。
语法

以下是startswith()方法的语法:

str.startswith(str, beg=0,end=len(string));

Copy after login

参数

  • str -- 这是要检查的字符串。
  • beg -- 这是可选的参数设置匹配边界的初始索引。
  • end -- 这是可选的参数设置匹配边界的结束索引。

返回值

如果找到匹配的字符串此方法返回true,否则为false。
例子

下面的例子显示了startswith()方法的使用。

#!/usr/bin/python

str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', 2, 4 );
print str.startswith( 'this', 2, 4 );

Copy after login

当我们运行上面的程序,它会产生以下结果:

True
True
False

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!