As a Python developer working on a text analysis project for a publishing company in Chicago, I came across a scenario where I needed to identify the second occurrence of specific keywords within articles. After researching and testing different methods, I found several effective solutions. In this tutorial, I will explain how to find the second occurrence of a substring in a Python string with examples.
Find the Second Occurrence of a Substring in a Python String
Let’s say you have a string that represents a list of American cities, separated by commas:
cities = "New York, Los Angeles, Chicago, Houston, New York, Philadelphia"Your task is to find the index of the second occurrence of “New York” in this string.
Read How to Convert a String to Date in Python?
Method 1: Use the find() Function
Python’s built-in find() function can locate the first occurrence of a substring within a string. To find the second occurrence, we can use find() twice.
Here’s how it works:
cities = "New York, Los Angeles, Chicago, Houston, New York, Philadelphia"
first_occurrence = cities.find("New York")
second_occurrence = cities.find("New York", first_occurrence + 1)
if second_occurrence != -1:
print(f"The second occurrence of 'New York' is at index {second_occurrence}")
else:
print("'New York' does not occur twice in the string")Output:
The second occurrence of 'New York' is at index 41I have executed the above example code and added the screenshot below.

In this code:
- We use
find()to locate the first occurrence of “New York” and store its index infirst_occurrence. - We call
find()again, but this time we start searching fromfirst_occurrence + 1. This ensures we find the next occurrence after the first one. - If
second_occurrenceis not -1 (which indicates the substring was not found), we print its index. Otherwise, we print a message indicating there is no second occurrence.
Check out How to Print Strings and Integers Together in Python?
Method 2: Use a Custom Function
We can also create a reusable function to find the nth occurrence of a substring. This is helpful if you need to find occurrences beyond just the second one.
def find_nth_occurrence(string, substring, n):
occurrences = 0
index = -1
while occurrences < n:
index = string.find(substring, index + 1)
if index == -1:
break
occurrences += 1
return index
cities = "New York, Los Angeles, Chicago, Houston, New York, Philadelphia"
second_new_york = find_nth_occurrence(cities, "New York", 2)
if second_new_york != -1:
print(f"The second occurrence of 'New York' is at index {second_new_york}")
else:
print("'New York' does not occur twice in the string")Output:
The second occurrence of 'New York' is at index 41I have executed the above example code and added the screenshot below.

In this custom find_nth_occurrence() function:
- We initialize
occurrencesto keep track of how many times we’ve found the substring, andindexto store the current index. - We start a loop that continues until we’ve found the nth occurrence (or run out of occurrences).
- Inside the loop, we use
find()to locate the next occurrence, starting from the previous index + 1. - If
find()returns -1, we break the loop since there are no more occurrences. - Otherwise, we increment
occurrencesand continue the loop. - Finally, we return the index of the nth occurrence (or -1 if not found).
Read How to Truncate a String to a Specific Length in Python?
Handle Edge Cases
When searching for occurrences, it’s important to consider edge cases. For example:
- What if the substring doesn’t occur at all?
- What if the substring occurs only once?
- What if the substring is empty?
Here’s an example that demonstrates handling these cases:
def find_second_occurrence(string, substring):
if not substring:
return "Substring cannot be empty"
first_occurrence = string.find(substring)
if first_occurrence == -1:
return "Substring not found"
second_occurrence = string.find(substring, first_occurrence + 1)
if second_occurrence == -1:
return "Substring occurs only once"
return second_occurrence
print(find_second_occurrence("John Smith, Jane Doe, John Doe", "John"))
print(find_second_occurrence("John Smith, Jane Doe, John Doe", "Doe"))
print(find_second_occurrence("John Smith, Jane Doe, John Doe", "Mike"))
print(find_second_occurrence("John Smith, Jane Doe, John Doe", ""))Output:
22
27
Substring not found
Substring cannot be emptyI have executed the above example code and added the screenshot below.

By adding these checks, we ensure our function provides informative feedback for different scenarios.
Check out How to Get the Last 4 Characters of a String in Python?
Conclusion
In this tutorial, I have explained how to find the second occurrence of a substring in a Python string. I covered some important methods like using the find() function and using the custom function. I also covered how to handle edge cases.
You may also like to read:
- How to Get the Last 3 Characters of a String in Python?
- How to Check if a Word is in a String in Python?
- How to Shuffle Characters in a String using Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.