Python endswith()方法
描述
Python endswith() 方法用於判斷字串是否以指定尾碼結尾,如果以指定尾碼結尾返回True,否則返回False。可選參數"start"與"end"為檢索字串的開始與結束位置。
語法
endswith()方法語法:
str.endswith(suffix[, start[, end]])
參數
- suffix -- 該參數可以是一個字串或者是一個元素。
- start -- 字串中的開始位置。
- end -- 字元中結束位置。
返回值
如果字串含有指定的尾碼返回True,否則返回False。
實例
以下實例展示了endswith()方法的實例:
實例(Python 2.0+)
#!/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