05-20-2022, 12:47 PM
7 Easy Steps to Redirect Your Standard Output to a Variable (Python)
5/5 – (1 vote)
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:
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!![[Image: 1f440.png]](https://s.w.org/images/core/emoji/13.1.0/72x72/1f440.png)
Step 1: Import libraries StringIO and sys
Import the two libraries and to access the standard output and store the string input-output stream.
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.
Step 3: Capture standard output using a StringIO object
Create a variable and assign a object to the variable to capture the standard output stream.
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 object created in the previous step to the standard output that is captured with .
Step 5: Print to the standard output
From this point onwards, anything that is printed using the statement by any function you call in your Python script is written in the object referred to by variable .
The following exemplifies the statement but you can do anything here:
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.
Step 7: Get and print the string from stdout
At this point, your string from the standard output is stored in the object in the variable. You can access it using the method.
Full Code
Here’s the full code snippet for ease of copy&paste:
You can also check this out on our interactive Jupyter notebook so you don’t have to try in your own shell:
![[Image: image-168.png]](https://blog.finxter.com/wp-content/uploads/2022/05/image-168.png)
Try it yourself: Click to run in Jupyter Notebook (Google Colab)
References:
Are you ready to up your Python skills? Join our free email academy — we’ve cheat sheets too!
5/5 – (1 vote)
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
Code:
my_result- Code:
from io import StringIO
- Code:
import sys
- Code:
tmp = sys.stdout
- Code:
my_result = StringIO()
- Code:
sys.stdout = my_result
- Code:
print('hello world') # output stored in my_result
- Code:
sys.stdout = tmp
- Code:
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!
![[Image: 1f440.png]](https://s.w.org/images/core/emoji/13.1.0/72x72/1f440.png)
Step 1: Import libraries StringIO and sys
Import the two libraries
Code:
StringIOCode:
sysCode:
from io import StringIO import sysBoth 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.
Code:
tmp = sys.stdoutCreate a variable and assign a
Code:
StringIOCode:
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
Code:
StringIOCode:
sys.stdoutCode:
sys.stdout = my_resultFrom this point onwards, anything that is printed using the
Code:
print()Code:
StringIOCode:
my_resultThe following exemplifies the
Code:
print('hello world')Code:
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.
Code:
sys.stdout = tmpAt this point, your string from the standard output is stored in the
Code:
StringIOCode:
my_resultCode:
StringIO.getvalue()Code:
print(result.getvalue())Here’s the full code snippet for ease of copy&paste:
Code:
# 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 worldYou can also check this out on our interactive Jupyter notebook so you don’t have to try in your own shell:
![[Image: image-168.png]](https://blog.finxter.com/wp-content/uploads/2022/05/image-168.png)
Try it yourself: Click to run in Jupyter Notebook (Google Colab)References:
Are you ready to up your Python skills? Join our free email academy — we’ve cheat sheets too!

