Let's take example string, 'I am programmer and I am doing coding. I am interested in it' and the target word (substring) is 'am'. I want to find nth value of it where it is located by the complete words, not indexes. For example, substring 'am' is located on 2nd, 6th and 10 position. I tried searching but all the results were relevant to finding indexes. One I found about nth value didn't work for me. Code was giving error on ':' of 'IF'.
parts= haystack.split(needle, n+1)
if len(parts)<=n+1:
return -1
return len(haystack)-len(parts[-1])-len(needle)
Do you have an optimum and simple solution for this. I am trying to just solve the problem with possible solutions and logics. Your cooperation will be well appreciated.