Python 字符串 endswith() 方法

實例

檢查字符串是否以標點符號 (.) 結尾:

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

運行實例

定義和用法

如果字符串以指定值結尾,則 endswith() 方法返回 True,否則返回 False。

語法

string.endswith(value, start, end)

參數值

參數 描述
value 必需。檢查字符串是否以之結尾的值。
start 可選。整數。規定從哪個位置開始檢索。
end 可選。整數。規定從哪個位置結束檢索。

更多實例

實例

檢查字符串是否以短語 "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)

運行實例