Python 字符串 endswith() 方法

定义和用法

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

实例

例子 1

检查字符串是否以标点符号 (.) 结尾:

txt = "Hello, welcome to my world."

x = txt.endswith(".")

print(x)

运行实例

例子 2

检查字符串是否以短语 "my world." 结尾:

txt = "Hello, welcome to my world."

x = txt.endswith("my world.")

print(x)

运行实例

例子 3

检查位置 5 至 11 是否以短语 "my world." 结尾:

txt = "Hello, welcome to my world."

x = txt.endswith("my world.", 5, 11)

print(x)

运行实例

语法

string.endswith(value, start, end)

参数

参数 描述
value 必需。检查字符串是否以之结尾的值。
start 可选。整数。规定从哪个位置开始检索。
end 可选。整数。规定从哪个位置结束检索。