Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức chuỗi python [] với sự trợ giúp của các ví dụ.
Phương thức
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 trả về text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
6 nếu một chuỗi kết thúc với hậu tố được chỉ định. Nếu không, nó trả về text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
7.Thí dụ
message = 'Python is fun'
# check if the message ends with fun
print[message.endswith['fun']]
# Output: True
Cú pháp của chuỗi endswith []
Cú pháp của
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 là:str.endswith[suffix[, start[, end]]]
Các tham số endswith []
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 có ba tham số:- Hậu tố - Chuỗi hoặc bộ tải hậu tố cần kiểm tra - String or tuple of suffixes to be checked
- Bắt đầu [Tùy chọn] - Vị trí bắt đầu trong đó hậu tố sẽ được kiểm tra trong chuỗi. [optional] - Beginning position where suffix is to be checked within the string.
- Kết thúc [Tùy chọn] - Vị trí kết thúc trong đó hậu tố sẽ được kiểm tra trong chuỗi. [optional] - Ending position where suffix is to be checked within the string.
Trả về giá trị từ endswith []
Phương pháp
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 trả về một boolean.- Nó trả về true nếu một chuỗi kết thúc với hậu tố được chỉ định.
- Nó trả về sai nếu một chuỗi không kết thúc với hậu tố được chỉ định.
Ví dụ 1: endswith [] không có tham số bắt đầu và kết thúc
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
Đầu ra
False True True
Ví dụ 2: endswith [] với tham số bắt đầu và kết thúc
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith['learn.', 7]
print[result]
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith['is', 7, 26]
# Returns False
print[result]
result = text.endswith['easy', 7, 26]
# returns True
print[result]
Đầu ra
True False True
Ví dụ 2: endswith [] với tham số bắt đầu và kết thúc
Chuyển tuple cho endswith []
Có thể truyền một hậu tố tuple cho phương thức
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 trong Python.Nếu chuỗi kết thúc bằng bất kỳ mục nào của tuple, text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
5 sẽ trả về đúng. Nếu không, nó trả về sai
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
text = "programming is easy"
result = text.endswith[['programming', 'python']]
# prints False
print[result]
result = text.endswith[['python', 'easy', 'java']]
#prints True
print[result]
# With start and end parameter
# 'programming is' string is checked
result = text.endswith[['is', 'an'], 0, 14]
# prints True
print[result]
Đầu ra
False True True
Ví dụ 2: endswith [] với tham số bắt đầu và kết thúc
Có một cách để xử lý chức năng
False True True3 để thực hiện là hoạt động ngược. Ý tưởng là đọc chuỗi, văn bản để thay thế và văn bản mới từ đầu đến đầu. Logic vẫn giữ nguyên.
Điều này dựa trên cách chuyển một chuỗi ngược bằng
False True True4.
Ví dụ:
>>> a = "123 456 123 456 123 456"
>>> print[a]
123 456 123 456 123 456
và chúng tôi muốn thay thế một lần cuối cùng, [hoặc hai, hoặc n] xảy ra của "123" bằng "ABC"
>>> res = a[::-1].replace["123"[::-1], "abc"[::-1], 1][::-1]
>>> print[res]
123 456 123 456 abc 456
Điều này có thể được đặt như một chức năng hoạt động chính xác như thay thế.
str.endswith[suffix[, start[, end]]]0
Execution:
str.endswith[suffix[, start[, end]]]1
Python cung cấp nhiều cách để tạo chuỗi một chuỗi. Điều này thường được gọi là "cắt lát". Đây là cú pháp: Where,str.endswith[suffix[, start[, end]]]
2False
True
True
5: Chỉ số bắt đầu của chuỗi con. Nhân vật tại chỉ số này được bao gồm trong chuỗi con. Nếu False
True
True
5 không được bao gồm, nó được giả định bằng 0.
False True True7: Chỉ số chấm dứt của chuỗi con. Nhân vật tại chỉ số này không được bao gồm trong chuỗi con. Nếu
False True True7 không được bao gồm hoặc nếu giá trị được chỉ định vượt quá độ dài chuỗi, nó được giả định là bằng chiều dài của chuỗi theo mặc định.
False True True9: Mỗi ký tự "bước" sau khi ký tự hiện tại được đưa vào. Giá trị mặc định là 1. Nếu không bao gồm
False True True9, nó được giả định là bằng 1.
Cách sử dụng cơ bản
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith['learn.', 7]
print[result]
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith['is', 7, 26]
# Returns False
print[result]
result = text.endswith['easy', 7, 26]
# returns True
print[result]
1: Nhận tất cả các ký tự từ False True True5 đến
False True True7 - 1
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith['learn.', 7]
print[result]
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith['is', 7, 26]
# Returns False
print[result]
result = text.endswith['easy', 7, 26]
# returns True
print[result]
4: Nhận tất cả các ký tự từ đầu chuỗi đến False True True7 - 1
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith['learn.', 7]
print[result]
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith['is', 7, 26]
# Returns False
print[result]
result = text.endswith['easy', 7, 26]
# returns True
print[result]
6: Nhận tất cả các ký tự từ False True True5 đến cuối chuỗi
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith['learn.', 7]
print[result]
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith['is', 7, 26]
# Returns False
print[result]
result = text.endswith['easy', 7, 26]
# returns True
print[result]
8: Nhận tất cả các ký tự từ False True True5 đến
False True True7 - 1, không bao gồm mọi ký tự
False True True9
Ví dụ
1. Nhận 5 ký tự đầu tiên của chuỗiGet the first 5 characters of a string
str.endswith[suffix[, start[, end]]]3
Output:
str.endswith[suffix[, start[, end]]]4
Lưu ý:
True False True2 Trả về kết quả tương tự như
True False True3
2. Lấy một chuỗi con dài 4 ký tự, bắt đầu từ ký tự thứ 3 của chuỗiGet a substring 4 characters long,starting from the 3rd character of the string
str.endswith[suffix[, start[, end]]]5
Output:
str.endswith[suffix[, start[, end]]]6
3. Nhận ký tự cuối cùng của chuỗiGet the last character of the string
str.endswith[suffix[, start[, end]]]7
Output:
str.endswith[suffix[, start[, end]]]8
Lưu ý rằng chỉ số
False True True5 hoặc
False True True7 có thể là một số âm. Một chỉ số âm có nghĩa là bạn bắt đầu đếm từ cuối chuỗi thay vì bắt đầu [từ bên phải sang trái].
Index -1 đại diện cho ký tự cuối cùng của chuỗi, -2 đại diện cho ký tự thứ hai đến cuối cùng, v.v.
4. Nhận 5 ký tự cuối cùng của chuỗiGet the last 5 characters of a string
str.endswith[suffix[, start[, end]]]9
Output:
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
05. Nhận một chuỗi con chứa tất cả các ký tự ngoại trừ 4 ký tự cuối cùng và ký tự thứ nhấtGet a substring which contains all characters except the last 4 characters and the 1st character
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
1Output:
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
26. Nhận mọi ký tự khác từ một chuỗiGet every other character from a string
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
3Output:
text = "Python is easy to learn."
result = text.endswith['to learn']
# returns False
print[result]
result = text.endswith['to learn.']
# returns True
print[result]
result = text.endswith['Python is easy to learn.']
# returns True
print[result]
4Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu