Latest web development tutorials

Python endswith()方法

Python 字符串 Python字符串


描述

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

語法

endswith()方法語法:

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

參數

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

返回值

如果字符串含有指定的後綴返回True,否則返回False。

實例

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

#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";
print str.endswith(suffix);
print str.endswith(suffix,20);

suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);

以上實例輸出結果如下:

True
True
True
False

Python 字符串 Python字符串