Python3 startswith()方法

Python3 字串 Python3 字串


描述

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

語法

startswith()方法語法:

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

參數

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

返回值

如果檢測到字串則返回True,否則返回False。

實例

以下實例展示了startswith()函數的使用方法:

React 實例

#!/usr/bin/python3 str = "this is string example....wow!!!" print (str.startswith( 'this' )) # 字串是否以 this 開頭 print (str.startswith( 'string', 8 )) # 從第八個字元開始的字串是否以 string 開頭 print (str.startswith( 'this', 2, 4 )) # 從第2個字元開始到第四個字元結束的字串是否以 this 開頭

以上實例輸出結果如下:

True
True
False

Python3 字串 Python3 字串