<p>Is there a Python class that wraps the <code>file</code> interface (read, write etc.) around a string? I mean something like the <code>stringstream</code> classes in C++.</p>

<p>I was thinking of using it to redirect the output of <code>print</code> into a string, like this</p>

<pre><code>sys.stdout = string_wrapper()
print "foo", "bar", "baz"
s = sys.stdout.to_string() #now s == "foo bar baz"
</code></pre>

<p>EDIT: This is a duplicate of <a href="https://stackoverflow.com/questions/141449/how-do-i-wrap-a-string-in-a-file-in-python">How do I wrap a string in a file in Python?</a></p>

Yes, there is StringIO:
```
```
import StringIO
import sys

sys.stdout = StringIO.StringIO()
print "foo", "bar", "baz"
s = sys.stdout.getvalue()
```
```