Open this example in the interactive library
Source code
from io import StringIO
buffer = StringIO()
buffer.write('first ')
buffer.write('second')
print(buffer.getvalue())Sample output
first second

Use a text buffer. A self-contained example using the Python standard library.
Open this example in the interactive library
from io import StringIO
buffer = StringIO()
buffer.write('first ')
buffer.write('second')
print(buffer.getvalue())first second