7 Easy Steps to Redirect Your Standard Output to a Variable (Python)

5/5 - (2 votes)

πŸ’¬ Question: How to redirect the standard output in Python and store it as a string in a variable?

This article will guide you through seven easy steps to solve this problem. As an overview, here’s the code in eight lines that stores the standard output in a variable my_result:

  1. from io import StringIO
  2. import sys
  3. tmp = sys.stdout
  4. my_result = StringIO()
  5. sys.stdout = my_result
  6. print('hello world') # output stored in my_result
  7. sys.stdout = tmp
  8. print(result.getvalue())

Let’s go over those steps one by one—we’ll examine the full code for copy&paste at the end of this article, so read on! πŸ‘€

Step 1: Import libraries StringIO and sys

Import the two libraries StringIO and sys to access the standard output and store the string input-output stream.

from io import StringIO 
import sys

Both modules are part of the standard library, so there is no need to install them with pip!

Step 2: Keep stdout in temporary variable

We’ll overwrite the standard output to catch everything written to it. In order to reset your code to the normal state, we need to capture the original standard output stream by introducing a temporary variable.

tmp = sys.stdout

Step 3: Capture standard output using a StringIO object

Create a variable and assign a StringIO object to the variable to capture the standard output stream.

my_result = StringIO()

Now, this object can store everything printed to the standard output. But we have to connect it first to the stdout!

Step 4: Assign Standard Output Stream to StringIO object

Assign the StringIO object created in the previous step to the standard output that is captured with sys.stdout.

sys.stdout = my_result

Step 5: Print to the standard output

From this point onwards, anything that is printed using the print() statement by any function you call in your Python script is written in the StringIO object referred to by variable my_result.

The following exemplifies the print('hello world') statement but you can do anything here:

print('hello world')

πŸ’‘ Note: No output appears on the screen anymore because the standard output is now redirected to the variable.

Step 6: Clean up by redirecting stdout to Python shell

Are you ready with capturing the output in the variable? Clean up by redirecting the standard output stream from the variable to the screen again.

sys.stdout = tmp

Step 7: Get and print the string from stdout

At this point, your string from the standard output is stored in the StringIO object in the my_result variable. You can access it using the StringIO.getvalue() method.


Full Code

Here’s the full code snippet for ease of copy&paste:

# Step 1
from io import StringIO
import sys

# Step 2
tmp = sys.stdout

# Step 3
my_result = StringIO()

# Step 4
sys.stdout = my_result

# Step 5
print('hello world')

# Step 6
sys.stdout = tmp

# Step 7
print('VARIABLE:', my_result.getvalue())
# hello world

You can also check this out on our interactive Jupyter notebook so you don’t have to try in your own shell:

🧩 Try it yourself: Click to run in Jupyter Notebook (Google Colab)


Are you ready to up your Python skills? Join our free email academy — we’ve cheat sheets too! πŸ™‚