Find the nth occurrence of substring in a string
Question: This seems like it should be pretty trivial, but I am new at Python and want
to do it the most Pythonic way.

I want to find the n'th occurrence of a substring in a string.

There's got to be something equivalent to what I WANT to do which is

`mystring.find("substring", 2nd)`

How can you achieve this in Python?


Answer: Here's a more Pythonic version of the straightforward iterative solution:

    
    
    def find_nth(haystack, needle, n):
        start = haystack.find(needle)
        while start >= 0 and n > 1:
            start = haystack.find(needle, start+len(needle))
            n -= 1
        return start
    

_Example:_

    
    
    >>> find_nth("foofoofoofoo", "foofoo", 2)
    6
    

If you want to find the nth _overlapping_ occurrence of `needle`, you can
increment by `1` instead of `len(needle)`, like this:

    
    
    def find_nth_overlapping(haystack, needle, n):
        start = haystack.find(needle)
        while start >= 0 and n > 1:
            start = haystack.find(needle, start+1)
            n -= 1
        return start
    

_Example:_

    
    
    >>> find_nth_overlapping("foofoofoofoo", "foofoo", 2)
    3
    

This is easier to read than Mark's version, and it doesn't require the extra
memory of the splitting version or importing regular expression module. It
also adheres to a few of the rules in the [Zen of
python](http://www.python.org/dev/peps/pep-0020/), unlike the various `re`
approaches:

  1. Simple is better than complex.
  2. Flat is better than nested.
  3. Readability counts.

