Python3 endswith()方法

Python3 字串 Python3 字串


描述

endswith() 方法用於判斷字串是否以指定尾碼結尾,如果以指定尾碼結尾返回 True,否則返回 False。可選參數 "start" 與 "end" 為檢索字串的開始與結束位置。

語法

endswith()方法語法:

str.endswith(suffix[, start[, end]])

參數

  • suffix -- 該參數可以是一個字串或者是一個元素。
  • start -- 字串中的開始位置。
  • end -- 字元中結束位置。

返回值

如果字串含有指定的尾碼返回 True,否則返回 False。

實例

以下實例展示了endswith()方法的實例:

實例

#!/usr/bin/python3 Str='zaixian example....wow!!!' suffix='!!' print (Str.endswith(suffix)) print (Str.endswith(suffix,20)) suffix='run' print (Str.endswith(suffix)) print (Str.endswith(suffix, 0, 19))

以上實例輸出結果如下:

True
True
False
False

Python3 字串 Python3 字串