How do you suppress output in Jupyter running IPython?

How can output to stdout be suppressed?

A semi-colon can be used to supress display of returned objects, for example

>>> 1+1
2

>>> 1+1;   # No output!

However, a function that prints to stdout is not affected by the semi-colon.

>>> print('Hello!')
Hello!

>>> MyFunction()
Calculating values...

How can the output from print / MyFunction be suppressed?


Solution 1:

Add %%capture as the first line of the cell. eg

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

This simply discards the output, but the %%capture magic can be used to save the output to a variable - consult the docs

Solution 2:

Suppress output

Put a ; at the end of a line to suppress the printing of output [Reference].

A good practice is to always return values from functions rather than printing values inside a function. In that case, you have the control; if you want to print the returned value, you can; otherwise, it will not be printed just by adding a ; after the function call.