str.startswith() ある文字列で始まるかどうか
最終更新:2026-09-22
str.startswith() ある文字列で始まるかどうか
str.startswith(prefix[, start[, end]]) は文字列が prefix で始まるかどうかを判定する。
| カテゴリ | 文字列メソッド |
|---|---|
| 種類 | 組み込み型メソッド |
| Python バージョン | all |
📝 構文
str.startswith(prefix[, start[, end]])
⚙️ 引数
| prefix 必須 | The prefix to check; a string or a tuple of strings. |
|---|---|
| start 任意 | Start index of the range (inclusive); None means the beginning.(デフォルト値:None) |
| end 任意 | End index of the range (exclusive); None means the end.(デフォルト値:None) |
戻り値:bool. True if the string starts with prefix within the range.
元のオブジェクトを変更するか:いいえ(新しいオブジェクトを返す)
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
True
False
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Q複数の接頭辞をチェックするには?
Aタプルを渡します:'hello.py'.startswith(('a', 'he')) は True を返します。prefix は文字列または文字列のタプルにできます。
Q大文字小文字を区別しますか?
Aはい。'Http'.startswith('http') は False です。先に lower() を呼べます。
Qstart/end パラメータはどう使いますか?
A検査範囲を区切ります(左閉・右開):'hello'.startswith('el', 1) はインデックス 1 から始まる 'ello' を検査し、True を返します。