StringIO. This Python type writes to strings. It uses a syntax similar to that for files. We can use print method calls, or invoke the write method.
Performance notes. StringIO has performance advantages for creating large strings. In some implementations, programs may run twice as fast.
We first import the io namespace. We create a new stream by calling io.StringIO: this returns a stream variable. Next, in a for-loop, I write some data to our stream.
Info We call the write method to output data to our stream. It is just stored in memory. A print() call also works.
Tip The getvalue method returns a string containing the data written to the in-memory buffer.
Finally We call the close method to prevent further writes to our StringIO stream. This is good housekeeping.
import io
out = io.StringIO()
# Print these string values in a loop.
for i in range(0, 100):
out.write("Value = ")
out.write(str(i))
out.write(" ")
# Get string and display first 20 character.
data = out.getvalue()
print(data[0:20])
out.close()Value = 0 Value = 1
Print. We do not need to use write() to write with StringIO. We can instead use print() and specify the optional "file" argument. The print method can target any compatible IO stream.
import io
out = io.StringIO()
# Print to StringIO stream, no end char.
print("Hello", file=out, end="")
# Print string contents to console.
print(out.getvalue())Hello
Performance. Is StringIO faster than a series of string appends? I tested a program that wrote large strings, appending strings many times.
Result I found StringIO was faster, in two Python implementations. In PyPy, StringIO was nearly twice as fast as a string append.
Note The if-check in the benchmark loops is just a simple sanity check. The strings must begin with the letter V.
import io
import time
print(time.time())
# 1. Use StringIO.
for x in range(0, 10000):
out = io.StringIO()
for i in range(0, 100):
out.write("Value = ")
out.write(str(i))
out.write(" ")
# Get string.
contents = out.getvalue()
out.close()
# Test first letter.
if contents[0] != 'V':
raise Error
print(time.time())
# 2. Use string appends.
for x in range(0, 10000):
data = ""
for i in range(0, 100):
data += "Value = "
data += str(i)
data += " "# Test first letter.
if data[0] != 'V':
raise Error
print(time.time())1404346870.027 [PyPy3 2.3.1]
1404346870.286 StringIO: 0.259 s
1404346870.773 Concat: 0.487 s
1404346852.222672 [Python 3.3]
1404346853.343738 StringIO: 1.121 s
1404346854.814824 Concat: 1.471 s
Summary. Many ways exist to append strings together. StringIO is faster than using direct string concats on large strings. It has more complex syntax than string appends.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.