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.
posted @   freepc  阅读(1328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示