Python startswith()方法

Python 字串 Python 字串


描述

Python startswith() 方法用於檢查字串是否是以指定子字串開頭,如果是則返回 True,否則返回 False。如果參數 beg 和 end 指定值,則在指定範圍內檢查。

語法

startswith()方法語法:

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

參數

  • str -- 檢測的字串。
  • strbeg -- 可選參數用於設置字串檢測的起始位置。
  • strend -- 可選參數用於設置字串檢測的結束位置。

返回值

如果檢測到字串則返回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 );

以上實例輸出結果如下:

True
True
False

Python 字串 Python 字串