python字符串-结尾判断(endswith)
endswith方法判断字符串是否以指定后缀结尾。
语法
S.endswith(suffix[, start[, end]]) -> bool
参数
- suffix: 指定的后缀字符串,也可以是一个元组。
- start: 可选参数,字符串的开始位置。
- end: 可选参数,字符串的结束位置。
返回值
- 包含指定后缀返回True,否则返回False。
示例
str = '我爱我的爸妈'
print(str.endswith('妈'))
print(str.endswith('爸妈'))
print(str.endswith('爸'))
print(str.endswith('爸', 0, len(str)-1))
# 可以使用元组作为参数,只要元组中包含结尾的字符串就返回True
print(str.endswith(('爸', '妈')))
True
True
False
True
True
help()
Help on built-in function endswith:
endswith(...) method of builtins.str instance
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?