Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcfind the last occurrence of substring in string in Python.
Bàn luận to find last occurrence of substring
Đôi khi, trong khi làm việc với các chuỗi, chúng ta cần tìm nếu một chuỗi con tồn tại trong chuỗi. Vấn đề này là khá phổ biến và giải pháp của nó đã được thảo luận nhiều lần trước đây. Biến thể của việc nhận được sự xuất hiện cuối cùng của chuỗi được thảo luận ở đây. Hãy để thảo luận về những cách nhất định mà chúng ta có thể tìm thấy sự xuất hiện cuối cùng của chuỗi con trong chuỗi trong Python. & NBSP;
Python3
Sử dụng Rindex () để tìm sự xuất hiện cuối cùng của chuỗi con
Phương thức Rindex () trả về sự xuất hiện cuối cùng của chuỗi con nếu có trong chuỗi. Hạn chế của chức năng này là nó ném ngoại lệ nếu không có chuỗi con trong chuỗi và do đó phá vỡ mã. & NBSP;
test_string = "GfG is best for CS and also best for Learning"
tar_word = "best"
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 282 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 285
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 286= The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 288 to find last occurrence of substring
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284
Python3
Sử dụng Rindex () để tìm sự xuất hiện cuối cùng của chuỗi con
Phương thức Rindex () trả về sự xuất hiện cuối cùng của chuỗi con nếu có trong chuỗi. Hạn chế của chức năng này là nó ném ngoại lệ nếu không có chuỗi con trong chuỗi và do đó phá vỡ mã. & NBSP;
test_string = "GfG is best for CS and also best for Learning"
tar_word = "best"
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 282 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 285
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 286= The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 288
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284
Python3
Sử dụng rfind () để tìm sự xuất hiện cuối cùng của chuỗi con
rfind () là phương pháp thay thế để thực hiện nhiệm vụ này. Ưu điểm mà hàm này cung cấp tốt hơn phương thức trên là, hàm này trả về một số -1 -1 nếu không tìm thấy cơ chất thay vì ném lỗi. & NBSP;
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 286= The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 289
Phương thức Rindex () trả về sự xuất hiện cuối cùng của chuỗi con nếu có trong chuỗi. Hạn chế của chức năng này là nó ném ngoại lệ nếu không có chuỗi con trong chuỗi và do đó phá vỡ mã. & NBSP;
test_string = "GfG is best for CS and also best for Learning"
tar_word = "best"
tar_word 6The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 285
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 282 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 285
=5=6==8=9
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 286= The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 288
tar_word 6The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284
Python3
Sử dụng rfind () để tìm sự xuất hiện cuối cùng của chuỗi con
Phương thức Rindex () trả về sự xuất hiện cuối cùng của chuỗi con nếu có trong chuỗi. Hạn chế của chức năng này là nó ném ngoại lệ nếu không có chuỗi con trong chuỗi và do đó phá vỡ mã. & NBSP;
test_string = "GfG is best for CS and also best for Learning"
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2809=The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2811
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2812=The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2814
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2815The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2816
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2817The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2818=The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2820The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2821"GfG is best for CS and also best for Learning"6=8The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2824The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2814The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2826
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2817The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2812The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283=The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2814
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2832=The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 2834
____10The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 281The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 282 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 283 The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 284The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 285
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 286= The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 288
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28