Python字符串startswith()
方法检查字符串是否以str
开头,可选地限制使用给定开始索引beg
和结束索引end
的匹配。
语法
以下是startswith()
方法的语法 -
str.startswith(str, beg = 0,end = len(string));
参数
- str - 这是要检查的字符串。
- beg - 这是设置匹配边界起始索引的可选参数。
- end - 这是设置匹配边界结束索引的可选参数。
返回值
- 如果找到匹配字符串,则此方法返回
true
,否则为false
。
示例
以下示例显示了startswith()
方法的用法 -
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.startswith( 'this' ))
print (str.startswith( 'string', 8 ))
print (str.startswith( 'this', 2, 4 ))
当运行上面的程序,它产生以下结果 -
True
True
False