共計 563 個字符,預(yù)計需要花費 2 分鐘才能閱讀完成。
endswith 函數(shù)用于判斷字符串是否以指定的后綴結(jié)尾,它的使用方法如下:
string.endswith(suffix[, start[, end]])
其中,string
是要檢查的字符串,suffix
是要檢查的后綴。start
和 end
是可選參數(shù),用于指定要檢查的字符串的起始和結(jié)束位置。
下面是一些使用示例:
string = "Hello, World!"
# 檢查字符串是否以 "World!" 結(jié)尾
result = string.endswith("World!")
print(result) # 輸出:True
# 檢查字符串是否以 "Hello" 開頭
result = string.endswith("Hello")
print(result) # 輸出:False
# 檢查字符串中從索引 2 開始的子字符串是否以 "World!" 結(jié)尾
result = string.endswith("World!", 2)
print(result) # 輸出:False
# 檢查字符串中從索引 2 到索引 13 的子字符串是否以 "World!" 結(jié)尾
result = string.endswith("World!", 2, 13)
print(result) # 輸出:True
注意:endswith
函數(shù)區(qū)分大小寫。如果要忽略大小寫,可以先將字符串轉(zhuǎn)換為小寫或大寫,然后再進(jìn)行判斷。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完