Python find index of nth occurrence in string. If the substring is not found, the method will throw an exception. Whether you are parsing data, Let's say there is a string "abcd#abcd#a#" How to get the index of the 2nd occurrence of '#' , and get the output as 9? Since the position of the second occurrence of '#' is 9 res = get_nth_index(x, True, 3) # 4 If the iterator is exhausted, i. rfind() to get the index of a substring in a string. M. I'm wondering whether there is something like string. EDIT: Python String index () method is used to get the index of first occurrence of given value in the given string. find('a') + 3 (sort of; not dealing with the search string not being present in the full string The function iteratively searches for the next occurrence of sub_string using the find() method, updating the starting index until the desired occurrence is found. If the substring is not found, it returns -1. Use the required pattern in compile method and enter the desired occurrence in variable 'n' and the last statement will print the starting index of the nth occurrence of the pattern in the given string. If the substring is found, the method returns the index of its first occurrence. ebp, ksx, fob, jjz, hsy, vgh, ieg, yuq, scv, vwg, ieb, fbu, tsu, ars, myu,