Trong hướng dẫn này, chúng ta tìm hiểu phương thức endswith() của chuỗi.
Phương thức endswith() trả về True nếu chuỗi kết thúc bằng substring cho trước, ngược lại False. Có 1 tham số bắt buộc và 2 tham số tùy chọn (start, end). Mặc định start=0, end=len-1.
Ví dụ
string = "tutorialspoint"
print(string.endswith('point'))
print(string.endswith('Point'))
print(string.endswith('tutorialspoint'))
Kết quả
True False True
Với start, end: endswith() kiểm tra trong phạm vi đó (không bao gồm end).
Ví dụ
string = "tutorialspoint"
print(string.endswith('point', 10, len(string)))
print(string.endswith('point', 8, len(string)))
print(string.endswith('tutorialspoint', 0, len(string) - 1))
Kết quả
False True False
Kết luận
endswith() kiểm tra hậu tố chuỗi, hỗ trợ giới hạn phạm vi start/end. Hãy bình luận nếu có thắc mắc.