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 を返します。
Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%