Python string class such as StringBuilder in C #?

Is there any string class in Python like StringBuilder in C #?

+61
python string
Mar 10 '10 at 5:08
source share
7 answers

There is no one-to-one correlation. For a really good article, see Effective String Concatenation in Python :

Creating long lines in a Python programming language can sometimes lead to very slow code execution. In this article, I explore the computational performance of various string concatenation methods.

+52
Mar 10 '10 at 5:11
source share

I used Oliver Crowe's code (a link given by Andrew Hare) and adapted it a bit to adapt Python 2.7.3. (using the timeit package). I ran on my personal computer, Lenovo T61, RAM 6 GB, Debian GNU / Linux 6.0.6 (compression).

Here is the result for 10,000 iterations:

 method1: 0.0538418292999 secs
 process size 4800 kb
 method2: 0.22602891922 secs
 process size 4960 kb
 method3: 0.0605459213257 secs
 process size 4980 kb
 method4: 0.0544030666351 secs
 process size 5536 kb
 method5: 0.0551080703735 secs
 process size 5272 kb
 method6: 0.0542731285095 secs
 process size 5512 kb

and for 5,000,000 iterations (method 2 was ignored because it slowly started slowly, like forever):

 method1: 5.88603997231 secs
 process size 37976 kb
 method3: 8.40748500824 secs
 process size 38024 kb
 method4: 7.96380496025 secs
 process size 321968 kb
 method5: 8.03666186333 secs
 process size 71720 kb
 method6: 6.68192911148 secs
 process size 38240 kb

It’s clear that the Python guys did a great job of optimizing string concatenation, and, as Hoar said, “premature optimization is the root of all evil” :-)

+22
Nov 12
source share

Python has several things that accomplish similar goals:

  • One common way to build large strings from chunks is to grow a list of strings and join it when you are done. This is a commonly used Python idiom.
    • To create rows containing formatted data, you will format separately.
  • For insertion and deletion at the character level, you save a list of strings of length one. (To do this from a string, you must call list(your_string) . You can also use UserString.MutableString .
  • (c)StringIO.StringIO is useful for things that otherwise accept a file, but to a lesser extent for general line-building.
+12
Mar 10 '10 at 6:21
source share

Using method 5 above (Pseudo File), we can get very good performance and flexibility.

 from cStringIO import StringIO class StringBuilder: _file_str = None def __init__(self): self._file_str = StringIO() def Append(self, str): self._file_str.write(str) def __str__(self): return self._file_str.getvalue() 

now uses it

 sb = StringBuilder() sb.Append("Hello\n") sb.Append("World") print sb 
+9
Nov 02 '13 at
source share

you can try StringIO or cStringIO

+4
Mar 10 '10 at 5:11
source share

Using compiler optimizers is fragile. The ratings associated with the accepted answer and the numbers given by Antoine Throne are not credible. Andrew Hare makes a mistake, including calling repr in his methods. This slows down all methods equally, but hides the real punishment when building a string.

Use join . He is very fast and reliable.

 $ ipython3 Python 3.5.1 (default, Mar 2 2016, 03:38:02) IPython 4.1.2 -- An enhanced Interactive Python. In [1]: values = [str(num) for num in range(int(1e3))] In [2]: %%timeit ...: ''.join(values) ...: 100000 loops, best of 3: 7.37 µs per loop In [3]: %%timeit ...: result = '' ...: for value in values: ...: result += value ...: 10000 loops, best of 3: 82.8 µs per loop In [4]: import io In [5]: %%timeit ...: writer = io.StringIO() ...: for value in values: ...: writer.write(value) ...: writer.getvalue() ...: 10000 loops, best of 3: 81.8 µs per loop 
+3
Apr 11 '16 at 18:26
source share

If you are looking for a quick method to concatenate strings in Python, you don't need a special StringBuilder class. Simple concatenation works just as well as it does in C #.

 resultString = "" resultString += "Append 1" resultString += "Append 2" 

See Antoine Throne answer for work results.

-one
Dec 31 '15 at 17:52
source share



All Articles