title: python: count and replace pattern in string without regex
body: <p>What is the non-regex equivalent of <code>subn</code>? I would like to replace a pattern in a string and count how many times was the pattern replaced.
  I ended up with:</p>
  
  <pre><code>def replacen(pat, repl, txt):
      txt2 = txt.replace(pat, repl)
      if (len(pat) != len(repl)):
          return (txt2, (len(txt) - len(txt2)) / (len(pat) - len(repl)))
      else:
          return (txt2, txt.count(pat))
  </code></pre>
  
  <p>Is there a more elegant solution?</p>