How to use lambda() with rlocate() function In Python
Here we are using the more_itertools library that provides us with rlocate() function that helps us to find the last occurrence of the substring in the given string.
Python3
import more_itertools as m test_string = "GfG is best for CS and also best for Learning" tar_word = "best" pred = lambda * x: x = = tuple (tar_word) print ( "The original string : " + str (test_string)) res = next (m.rlocate(test_string, pred = pred, window_size = len (tar_word))) print ( "Index of last occurrence of substring is : " + str (res)) |
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28
Python | Find last occurrence of substring
Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let’s discuss certain ways in which we can find the last occurrence of substring in string in Python.
Contact Us