Python字串endswith()
方法判斷字串以指定的子字串為字尾(結尾),如果是則返回True
,否則返回False
,可選地限制由給定範圍索引start
和end
來匹配。
語法
以下是endswith()
方法的語法 -
str.endswith(suffix[, start[, end]])
引數
返回值
TRUE
,否則為FALSE
。範例
以下範例顯示了endswith()
方法的用法。
#! /usr/bin/env python
#coding=utf-8
# save file: logical_operators_example.py
str='Welcome to yiibai python toturitals!'
suffix='s!'
print (str.endswith(suffix))
print (str.endswith(suffix,20))
suffix='python'
print (str.endswith(suffix))
print (str.endswith(suffix, 0, len('Welcome to yiibai python')))
當執行上面的程式,它產生以下結果 -
True
True
False
True