Python 的 str.endswith(~) 方法返回 boolean 指示字符串是否以指定的 suffix 結尾。
參數
1. suffix | string
要在源字符串中檢查的後綴。
2. start | int | optional
要開始檢查的源字符串的索引位置(包含)。默認情況下,start=0 。
3. end | int | optional
要停止檢查的源字符串的索引位置(不包括)。默認情況下,end = start + len(suffix) 。
返回值
單個 boolean 指示源字符串是否以指定的 suffix 結尾。
例子
基本用法
檢查源字符串 'abc def' 是否以後綴 'ef' 結尾:
x = "abc def"
x.endswith("ef")
True
啟動參數
要檢查源字符串 'abc def' 是否以後綴 'def' 結尾,從索引位置 5 開始:
y = "abc def"
y.endswith("def", 5)
False
搜索從索引位置 5 開始,該位置位於 'e' 。由於 'ef' 不以提供的後綴 'def' 結尾,因此返回 False。
結束參數
檢查源字符串 'abc def' 是否以後綴 'bc' 結尾,從索引位置 0 開始,到索引位置 3 結束(不包括):
z = "abc def"
z.endswith("bc", 0, 3)
True
搜索的起始位置是索引位置 0 ( 'a' ),搜索在索引位置 3 ( ' ' ) 之前結束。因此,我們檢查字符串 'abc' 是否以後綴 'bc' 結尾,並返回 True 。
相關用法
- Python String endswith()用法及代碼示例
- Python String encode方法用法及代碼示例
- Python String encode()用法及代碼示例
- Python String expandtabs()用法及代碼示例
- Python String expandtabs方法用法及代碼示例
- Python String count方法用法及代碼示例
- Python String isnumeric方法用法及代碼示例
- Python String Center()用法及代碼示例
- Python String zfill方法用法及代碼示例
- Python String rstrip方法用法及代碼示例
- Python String decode()用法及代碼示例
- Python String count()用法及代碼示例
- Python String join()用法及代碼示例
- Python String casefold()用法及代碼示例
- Python String isalnum()用法及代碼示例
- Python String rsplit()用法及代碼示例
- Python String isidentifier()用法及代碼示例
- Python String startswith()用法及代碼示例
- Python String rjust方法用法及代碼示例
- Python String rpartition()用法及代碼示例
- Python String rpartition方法用法及代碼示例
- Python String ljust方法用法及代碼示例
- Python String splitlines()用法及代碼示例
- Python String upper()用法及代碼示例
- Python String isprintable()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Python String | endswith method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
