コース推薦:

インスタンス

Python 字符串 endswith() メソッド

txt = "Hello, welcome to my world."
文字列がピunctuation (.) で終わるかどうかを確認:
print(x)

実行インスタンス

x = txt.endswith(".")

定義と使用法

指定された値で終わる場合、endswith() メソッドは True を返し、そうでない場合は False を返します。

文法文字列.endswith( スタート.endswith( エンド,

)

引数 説明
必須。指定された値で終わるかどうかを確認する値です。
スタート オプション。整数。検索を開始する位置を指定します。
エンド オプション。整数。検索を終了する位置を指定します。

さらに例

インスタンス

文字列が "my world." で終わるかどうかを確認:

txt = "Hello, welcome to my world."
x = txt.endswith("my world.")
print(x)

実行インスタンス

インスタンス

位置 5 から 11 までの文字列が "my world." で終わるかどうかを確認:

txt = "Hello, welcome to my world."
x = txt.endswith("my world.", 5, 11)
print(x)

実行インスタンス