溫馨提示×

python中endswith函數怎么使用

小億
148
2023-11-23 04:26:15
欄目: 編程語言

endswith函數用于判斷字符串是否以指定的后綴結尾,它的使用方法如下:

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

其中,string是要檢查的字符串,suffix是要檢查的后綴。startend是可選參數,用于指定要檢查的字符串的起始和結束位置。

下面是一些使用示例:

string = "Hello, World!"

# 檢查字符串是否以"World!"結尾
result = string.endswith("World!")
print(result)  # 輸出:True

# 檢查字符串是否以"Hello"開頭
result = string.endswith("Hello")
print(result)  # 輸出:False

# 檢查字符串中從索引2開始的子字符串是否以"World!"結尾
result = string.endswith("World!", 2)
print(result)  # 輸出:False

# 檢查字符串中從索引2到索引13的子字符串是否以"World!"結尾
result = string.endswith("World!", 2, 13)
print(result)  # 輸出:True

注意:endswith函數區分大小寫。如果要忽略大小寫,可以先將字符串轉換為小寫或大寫,然后再進行判斷。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女