當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python String endswith()用法及代碼示例

如果字符串以給定的後綴結尾,則endswith()方法返回True,否則返回False。

用法:

str.endswith(suffix, start, end)

參數:


  • suffix:後綴隻不過是需要檢查的字符串。
    start :字符串中需要檢查後綴的起始位置。
    end:字符串中需要檢查後綴的結束位置。

注意:如果未提供開始和結束索引,則默認情況下它將0和length-1用作開始和結束索引,而我們的搜索中不包括結束索引。

返回值:

It returns True if string ends with the given
suffix otherwise returns False. 

代碼1

# Python code shows the working of 
# .endswith() function 
  
text = "geeks for geeks."
  
# returns False 
result = text.endswith('for geeks') 
print (result) 
  
# returns True 
result = text.endswith('geeks.') 
print (result) 
  
# returns True 
result = text.endswith('for geeks.') 
print (result) 
  
# returns True 
result = text.endswith('geeks for geeks.') 
print (result)

輸出:

False
True
True
True


代碼2

# Python code shows the working of 
# .endswith() function 
  
text = "geeks for geeks."
  
# start parameter:10 
result = text.endswith('geeks.', 10) 
print(result) 
  
# Both start and end is provided 
# start:10, end:15 
# Returns False 
result = text.endswith('geeks', 10, 15) 
print result 
  
# returns True 
result = text.endswith('geeks', 10, 14) 
print result

輸出:

True
True
False


相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 String endswith() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。