Dev HelpersOpen library
← Back

Capture standard output

Capture standard output. A self-contained example using the Python standard library.

Open this example in the interactive library

Source code

from contextlib import redirect_stdout
from io import StringIO
output = StringIO()
with redirect_stdout(output):
    print('hello')
print(repr(output.getvalue().strip()))

Sample output

'hello'