无涯教程-Python3 - endswith(suffix ,begin=0,end=len(string))函数

113 阅读1分钟

Python的 endswith()方法返回以指定子字符串结尾的字符串的true,否则返回false。

endswith - 语法

endswith(suffix[, start[, end]])

endswith - 参数

  • suffix - 一个子字符串
  • start   -  范围的开始索引
  • end     -  范围的最后一个索引

开始和结束两个参数都是可选的。

endswith - 返回类型

它返回布尔值True或False。

一个返回true的简单示例,因为它以点(.)结尾。

# Python endswith() function example
# 变量声明
str = "Hello this is javatpoint."
isends = str.endswith(".")
# 显示结果
print(isends)

输出

True

它返回false,因为字符串不以is结尾。

# Python endswith() function example
# 变量声明
str = "Hello this is javatpoint."
isends = str.endswith("is")
# 显示结果
print(isends)

输出

False

在这里,无涯教程提供方法开始搜索的范围的起始索引。

# Python endswith() function example
# 变量声明
str = "Hello this is javatpoint."
isends = str.endswith("is",10)
# 显示结果
print(isends)

输出

False

它返回true,因为第三个参数在索引13处停止了该方法。

# Python endswith() function example
# 变量声明
str = "Hello this is javatpoint."
isends = str.endswith("is",0,13)
# 显示结果
print(isends)

输出

True

参考链接

www.learnfk.com/python3/pyt…