def findnth(haystack, needle, n):
    parts = haystack.split(needle, n + 1)
if len(parts) <= n + 1:
    return -1
return len(haystack) - len(parts[-1]) - len(needle)
Find the nth occurrence of substring in a string