Sự mô tả
Phương thức chuỗi python endswith [] trả về true nếu chuỗi kết thúc bằng hậu tố được chỉ định, nếu không hãy trả về sai tùy chọn hạn chế khớp với các chỉ số đã cho bắt đầu và kết thúc.endswith[] returns True if the string ends with the specified suffix, otherwise return False optionally restricting the matching with the given indices start and end.
Cú pháp
str.endswith[suffix[, start[, end]]]
Thông số
Hậu tố - Đây có thể là một chuỗi hoặc cũng có thể là một bộ hậu tố để tìm kiếm. − This could be a string or could also be a tuple of suffixes to look for.
Bắt đầu - lát cắt bắt đầu từ đây. − The slice begins from here.
Kết thúc - các lát kết thúc ở đây. − The slice ends here.
Giá trị trả về
Đúng nếu chuỗi kết thúc với hậu tố được chỉ định, nếu không là sai.
Thí dụ
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]
Kết quả
True True True False
python_strings.htm
Nhân vật dòng mới trong Python là \ n. Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.
Làm thế nào để bạn tìm thấy kết thúc của một chuỗi?
Phương thức EndSwith [] xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi được chỉ định, trả về đúng hay sai khi thích hợp.
Nhân vật dòng mới trong Python là \ n. Nó được sử dụng để chỉ ra sự kết thúc của một dòng văn bản.
Làm thế nào để bạn tìm thấy kết thúc của một chuỗi?
Phương thức EndSwith [] xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi được chỉ định, trả về đúng hay sai khi thích hợp.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Python3
Đọc
Bàn luận
Output:
Welcome to GeeksforGeeks
Theo mặc định, hàm print [] của Python kết thúc bằng một dòng mới. Một lập trình viên có nền C/C ++ có thể tự hỏi làm thế nào để in mà không có dòng mới. Chức năng Python Python Print [] đi kèm với một tham số gọi là ‘kết thúc. Theo mặc định, giá trị của tham số này là ‘\ n, tức là ký tự dòng mới. & Nbsp;
Ví dụ 1:
Python3
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
Welcome to GeeksforGeeks1
True True True False5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
Welcome to GeeksforGeeks4
True True True False1
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
True True True False4
True True True False1
Output:
Python@GeeksforGeeks
Ở đây, chúng ta có thể kết thúc một câu lệnh in với bất kỳ ký tự/chuỗi nào bằng tham số này. & NBSP;
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]7
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]8
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
True True True False0
True True True False1
Python3
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
True True True False4
True True True False5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
True True True False0
True True True False1
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
Python@GeeksforGeeks2
True True True False1
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
GFG 09-12-2016 Red,Green,Blue@geeksforgeeks6
Python@GeeksforGeeks3
GFG 09-12-2016 Red,Green,Blue@geeksforgeeks8
Python@GeeksforGeeks3
word = "Help"
word[1:-1] # But I want to grab up to end of string!
word[1:len[word]] # Works but is there anything better?
0Python@GeeksforGeeks5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
word = "Help"
word[1:-1] # But I want to grab up to end of string!
word[1:len[word]] # Works but is there anything better?
3True True True False5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
word = "Help"
word[1:-1] # But I want to grab up to end of string!
word[1:len[word]] # Works but is there anything better?
6True True True False1
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
word[1:]
0Python@GeeksforGeeks3
word[1:]
2Python@GeeksforGeeks3
word[1:]
4Python@GeeksforGeeks5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
word[1:]
7True True True False5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]9
Welcome to GeeksforGeeks4
True True True False1
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]5
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]6
>>> word = "Help"
>>> word[-3:]
'elp'
4True True True False1
Ví dụ 2: & nbsp;
GFG 09-12-2016 Red,Green,Blue@geeksforgeeks
Phương thức chuỗi
Thí dụ
Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu [.]:
TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."
x = txt.endswith ["."]
print[x]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức
>>> word = "Help"
>>> word[-3:]
'elp'
6 trả về true nếu chuỗi kết thúc bằng giá trị được chỉ định, nếu không thì sai.Cú pháp
String.endswith [giá trị, bắt đầu, kết thúc]
Giá trị tham số
giá trị | Yêu cầu. Giá trị để kiểm tra xem chuỗi kết thúc với |
bắt đầu | Không bắt buộc. Một số nguyên chỉ định tại vị trí bắt đầu tìm kiếm |
chấm dứt | Không bắt buộc. Một số nguyên chỉ định tại vị trí để kết thúc tìm kiếm |
Nhiều ví dụ hơn
Thí dụ
Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu [.]:
TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."
x = txt.endswith ["."]
Hãy tự mình thử »
Hãy tự mình thử »
Thí dụ
Kiểm tra xem chuỗi kết thúc bằng dấu chấm câu [.]:
TXT = "Xin chào, Chào mừng bạn đến với thế giới của tôi."
x = txt.endswith ["."]
Hãy tự mình thử »
Hãy tự mình thử »
Phương thức chuỗi
33
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Với các chỉ số chuỗi, có cách nào để cắt vào cuối chuỗi mà không cần sử dụng
>>> word = "Help"
>>> word[-3:]
'elp'
7 không?Các chỉ số tiêu cực bắt đầu từ cuối, nhưng
>>> word = "Help"
>>> word[-3:]
'elp'
8 bỏ qua ký tự cuối cùng.word = "Help"
word[1:-1] # But I want to grab up to end of string!
word[1:len[word]] # Works but is there anything better?
mkrieger1
15.9k4 Huy hiệu vàng47 Huy hiệu bạc57 Huy hiệu Đồng4 gold badges47 silver badges57 bronze badges
hỏi ngày 18 tháng 3 năm 2012 lúc 9:31Mar 18, 2012 at 9:31
3
Thay vào đó bạn có thể thử sử dụng:
word[1:]
Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:33Mar 18, 2012 at 9:33
Uku loskituku loskitUku Loskit
39,8K9 Huy hiệu vàng88 Huy hiệu bạc92 Huy hiệu Đồng9 gold badges88 silver badges92 bronze badges
0
Hoặc thậm chí:
>>> word = "Help"
>>> word[-3:]
'elp'
Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:37Mar 18, 2012 at 9:37
Monkutmonkutmonkut
40.4K23 Huy hiệu vàng120 Huy hiệu bạc148 Huy hiệu đồng23 gold badges120 silver badges148 bronze badges
Tôi thấy mình cần chỉ định chỉ số cuối là một biến đầu vào trong một hàm. Trong trường hợp đó, bạn có thể thực hiện
>>> word = "Help"
>>> word[-3:]
'elp'
9. Ví dụ:def slice[val,start=1,stop=None]
return val[start:stop]
word = "Help"
slice[word] # output: 'elp'
Đã trả lời ngày 30 tháng 4 năm 2016 lúc 21:29Apr 30, 2016 at 21:29
Bob Baxleybob BaxleyBob Baxley
3,4451 Huy hiệu vàng21 Huy hiệu bạc28 Huy hiệu đồng1 gold badge21 silver badges28 bronze badges
1
Vâng, tất nhiên, bạn nên:
word[1:]
Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:34Mar 18, 2012 at 9:34
DenisdenisDenis
6.7397 Huy hiệu vàng37 Huy hiệu bạc58 Huy hiệu Đồng7 gold badges37 silver badges58 bronze badges
Bạn đang tìm kiếm điều này?
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]1
Đã trả lời ngày 18 tháng 3 năm 2012 lúc 9:34Mar 18, 2012 at 9:34
DenisdenisRik Poggi
6.7397 Huy hiệu vàng37 Huy hiệu bạc58 Huy hiệu Đồng6 gold badges63 silver badges81 bronze badges
Bạn đang tìm kiếm điều này?
word[1:]
Ở đây bạn đang chỉ định rằng bạn muốn các ký tự từ INDEX 1, đây là ký tự thứ hai của chuỗi của bạn, cho đến khi chỉ mục cuối cùng ở cuối. Điều này có nghĩa là bạn chỉ cắt ký tự ở chỉ mục đầu tiên của chuỗi, trong trường hợp này là 'H'. In ấn này sẽ dẫn đến: 'ELP'
Không chắc đó có phải là những gì bạn đã theo đuổi.
Đã trả lời ngày 28 tháng 4 năm 2019 lúc 19:15Apr 28, 2019 at 19:15
Đôi khi rất hữu ích để lưu chỉ mục, ví dụ: sử dụng nó trên các biến khác hoặc chuyển nó đến các chức năng. Ở đây, các đối tượng
def slice[val,start=1,stop=None]
return val[start:stop]
word = "Help"
slice[word] # output: 'elp'
0 có hữu ích.#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]3
Đã trả lời ngày 27 tháng 5 năm 2021 lúc 16:45May 27, 2021 at 16:45
Japamatjapamatjapamat
5454 Huy hiệu bạc5 Huy hiệu Đồng4 silver badges5 bronze badges
#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith[suffix] print str.endswith[suffix,20] suffix = "is"; print str.endswith[suffix, 2, 4] print str.endswith[suffix, 2, 6]4
'Help'
Tôi hy vọng cái này sẽ giúp bạn
Tunaki
128K45 Huy hiệu vàng323 Huy hiệu bạc406 Huy hiệu Đồng45 gold badges323 silver badges406 bronze badges
Đã trả lời ngày 3 tháng 6 năm 2016 lúc 10:11Jun 3, 2016 at 10:11