Skip to content

Instantly share code, notes, and snippets.

@DahlitzFlorian
Created November 29, 2022 15:59
Show Gist options
  • Save DahlitzFlorian/1568c32797067ec0a41dcbf2418fd61a to your computer and use it in GitHub Desktop.
Save DahlitzFlorian/1568c32797067ec0a41dcbf2418fd61a to your computer and use it in GitHub Desktop.
Provide the full working example for a user asking a question on the video "How to Capture What Is Written to stdout in Python"
import contextlib
import io
captured_output = io.StringIO()
with contextlib.redirect_stdout(captured_output):
help(pow)
captured_string = captured_output.getvalue()
print(captured_string.upper())
# Output from the terminal:
"""
$ python t.py
HELP ON BUILT-IN FUNCTION POW IN MODULE BUILTINS:
POW(X, Y, Z=NONE, /)
EQUIVALENT TO X**Y (WITH TWO ARGUMENTS) OR X**Y % Z (WITH THREE ARGUMENTS)
SOME TYPES, SUCH AS INTS, ARE ABLE TO USE A MORE EFFICIENT ALGORITHM WHEN
INVOKED USING THE THREE ARGUMENT FORM.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment