str.endswith() 特定の文字列で終わるかどうか
最終更新:2026-09-22
str.endswith() 特定の文字列で終わるかどうか
str.endswith(suffix[, start[, end]]) は文字列が suffix で終わるかどうかを判定する。
| カテゴリ | 文字列メソッド |
|---|---|
| 種類 | 組み込み型メソッド |
| Python バージョン | all |
📝 構文
str.endswith(suffix[, start[, end]])
⚙️ 引数
| suffix 必須 | The suffix 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 ends with suffix within the range.
元のオブジェクトを変更するか:いいえ(新しいオブジェクトを返す)
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
True
False
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Q複数の接尾辞を一度にチェックするには?
Asuffix にタプルを渡します:'a.py'.endswith(('.py', '.txt')) は True を返します。suffix は文字列または文字列のタプルにできます。
Qstart/end パラメータは何をしますか?
A検査範囲を区切ります(左閉・右開)。例:'hello.py'.endswith('.py', 0, 8) は最初の 8 文字 'hello.p' だけを検査し、False を返します。
Qendswith() は大文字小文字を区別しますか?
Aはい。'a.PY'.endswith('.py') は False です。先に lower() を呼ぶ必要があります。
Q接尾辞として空文字列を渡すとどうなりますか?
Aすべての文字列は空文字列で終わるため、'abc'.endswith('') は True を返します。