Python: Assign print output to a variable
To answer the question more generaly how to redirect standard output to a variable ?
do the following :
from io import StringIOimport sysresult = StringIO()sys.stdout = resultresult_string = result.getvalue()
If you need to do that only in some function do the following :
old_stdout = sys.stdout # your function containing the previous linesmy_function()sys.stdout = old_stdout