avatar Bite 217. Capture stdout

Sometimes you need to capture stdout in your script. Python makes it easy with contextlib's redirect_stdout.

In this Bite you will use it to get the length of a help text as returned by help.

Complete get_len_help_text which receives a builtin. Run help against this builtin and capture its output in a variable and return its length (number of chars). If a non-builtin is passed into the function, raise a ValueError (hint: you can use BuiltinFunctionType to check this).

Here you see the function in action:

>>> from helplen import get_len_help_text
>>> get_len_help_text(max)
>>> get_len_help_text(pow)
>>> get_len_help_text('bogus')

Good luck and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #146 to crack this Bite?
Resolution time: ~19 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.67 on a 1-10 difficulty scale.
» You can do it! 😌

We use Python 3.8