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