Home
Map
StringIO ExamplesUse the StringIO class from the io namespace. StringIO writes to strings like files.
Python
This page was last reviewed on Sep 26, 2022.
StringIO writes to strings. This Python type 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.
Detail We call the write method to output data to our stream. It is just stored in memory. A print() call also works.
Detail The getvalue method returns a string containing the data written to the in-memory buffer.
Detail 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.
Console
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.
if
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.