title: Effective way to iteratively append to a string in Python?
body: <p>I'm writing a Python function to split text into words, ignoring specified punctuation. Here is some working code. I'm not convinced that constructing strings out of lists (buf = [] in the code) is efficient though. Does anyone have a suggestion for a better way to do this?</p>
  
  <pre><code>def getwords(text, splitchars=' \t|!?.;:"'):
      """
      Generator to get words in text by splitting text along specified splitchars
      and stripping out the splitchars::
  
        &gt;&gt;&gt; list(getwords('this is some text.'))
        ['this', 'is', 'some', 'text']
        &gt;&gt;&gt; list(getwords('and/or'))
        ['and', 'or']
        &gt;&gt;&gt; list(getwords('one||two'))
        ['one', 'two']
        &gt;&gt;&gt; list(getwords(u'hola unicode!'))
        [u'hola', u'unicode']
      """
      splitchars = set(splitchars)
      buf = []
      for char in text:
          if char not in splitchars:
              buf.append(char)
          else:
              if buf:
                  yield ''.join(buf)
                  buf = []
      # All done. Yield last word.
      if buf:
          yield ''.join(buf)
  </code></pre>