Python 字符串 endswith() 方法

实例

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

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

定义和用法

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


语法

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

更多实例

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

  1. txt = "Hello, welcome to my world."
  2. x = txt.endswith("my world.")
  3. print(x)
实例

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

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

分类导航