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 文字列

その他の拡張