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