Latest web development tutorials

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字符串