title: Counting substrings in Python - more efficient approach?
body: <p>So I've been learning Python for a couple months now.  I came across an exercise that wants you to count how many times a sub-string appears in a string.  I searched, but couldn't find quite the exact answer I was looking for.  Here is the code I wrote, that is functional.  However, it does take a second due to the exception.  I chose to use string.index because the -1 value from string.find in some words would mess up the starting point.  What is the more efficient way, without importing other modules, etc.  E.g. in more basic Python, such as the code I wrote.</p>
  
  <pre><code>word = "banana"
  sub = "ba"
  start = 0
  ix = 0
  count = 0
  end = None
  
  if end is None:
      end = len(word)
  while start &lt; end:
      if sub in word:
          try:
              ix = word.index(sub, start, end)
          except:
              break
          ix += 1
          start = ix + 1
          count += 1
  print(count)
  </code></pre>
  
  <p>Thanks!</p>