在Python中,要判断一个字符串是否以特定的后缀结尾,可以使用 `str.endswith()` 方法。以下是该方法的语法和示例:
str.endswith(suffix, start=0, end=len(str)) -> bool
`suffix`:要检查的后缀字符串,可以是单个字符串或元组。
`start`:可选参数,指定检查的起始位置索引,默认为0。
`end`:可选参数,指定检查的结束位置索引,默认为字符串的长度。
示例:
str1 = "Hello World!"
ends_with_world = str1.endswith("World!") 返回 True
ends_with_python = str1.endswith("Python") 返回 False
print(ends_with_world) 输出:True
print(ends_with_python) 输出:False