import StringIO f = StringIO.StringIO() f.write("Hello World\n") ... s = f.getvalue() # Get saved string value