Python字串endswith()方法

2019-10-16 23:06:18

Python字串endswith()方法判斷字串以指定的子字串為字尾(結尾),如果是則返回True,否則返回False,可選地限制由給定範圍索引startend來匹配。

語法

以下是endswith()方法的語法 -

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

引數

  • 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