Python中字符串匹配函数startswith()函数

1.函数用途含义

Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

2.用法

Str.startswith(str, beg=0,end=len(string));
  • Str是需要匹配的字符串
  • str是待检测子字符串
  • beg默认为0表示从第一个字符开始匹配
  • end表示终止匹配的位置

3.实例

#!/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 );

out:
True
True
False
posted @ 2019-08-29 20:54  睿晞  阅读(3758)  评论(0编辑  收藏  举报