endswith函数用于判断字符串是否以指定的后缀结尾,它的使用方法如下:
string.endswith(suffix[, start[, end]])其中,string是要检查的字符串,suffix是要检查的后缀。start和end是可选参数,用于指定要检查的字符串的起始和结束位置。
下面是一些使用示例:
string = "Hello, World!"# 检查字符串是否以"World!"结尾result = string.endswith("World!")print(result) # 输出:True# 检查字符串是否以"Hello"开头result = string.endswith("Hello")print(result) # 输出:False# 检查字符串中从索引2开始的子字符串是否以"World!"结尾result = string.endswith("World!", 2)print(result) # 输出:False# 检查字符串中从索引2到索引13的子字符串是否以"World!"结尾result = string.endswith("World!", 2, 13)print(result) # 输出:True注意:endswith函数区分大小写。如果要忽略大小写,可以先将字符串转换为小写或大写,然后再进行判断。

