Suppressing Output in Jupyter Notebooks Using %%capture Magic Command

Dec 02, 2025 · Programming · 18 views · 7.8

Keywords: Jupyter | IPython | %%capture | output suppression | stdout

Abstract: This article discusses methods to suppress output in Jupyter Notebooks running IPython. It covers the use of semicolons to suppress display of returned objects and introduces the %%capture magic command to handle stdout output from print statements and functions. Best practices for function design are also highlighted.

Introduction

Jupyter Notebooks, running IPython kernels, are widely used for interactive computing and data analysis. A common requirement is to suppress output from code cells, especially when dealing with functions that print to stdout.

Using Semicolons to Suppress Display

In IPython, adding a semicolon at the end of a line can suppress the display of the returned object. For example, 1+1; will not show the result. However, this method does not affect output to stdout from functions like print() or custom functions that print internally.

Suppressing stdout Output with %%capture

To suppress output from print statements or functions that write to stdout, the %%capture magic command can be used. This command captures the output and optionally stores it in a variable or discards it.

For instance:

%%capture
print('Hello')
MyFunction()

This will suppress all output from the cell. The %%capture command can also save the output to a variable for later use.

Best Practices

As suggested in the supplementary answer, a good practice is to design functions that return values rather than printing them. This gives the user control over when to display the output.

Conclusion

In summary, to suppress output in Jupyter Notebooks, use semicolons for returned objects and %%capture for stdout output. Adopting return-based function design can further enhance control over output display.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.