by 8/18/2005 04:47:00 PM 0 comments

Efficient String Concatenation in Python

If you've ever created long strings in Python, you may have noticed that string concatenation is slow. There's a great site that tries out the different methods for concatenating strings in python at http://www.skymind.com/~ocrow/python_string/. In a nutshell, I've found this one works best for my operations:
def method5():
  # Example that concatenates numbers
  from cStringIO import StringIO
  file_str = StringIO()
  for num in xrange(loop_count):
    file_str.write(`num`)

  return file_str.getvalue()

Brian Schlining

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments: