Latest web development tutorials

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='w3big 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字符串