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.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM