9

In the documentation at IPython Tips & Tricks, it says to put a semicolon (;) at the end of a command to suppress its output. This does not seem to work in my case as even a

print('Hello');

outputs

Hello

Do I have the wrong idea of output suppression or is this a bug? This is especially annoying when working in PuDB, as it flashes horribly in my case as I press 'next' or 'step into'.

P.S.: The output is neither on my Ubuntu IPython 0.10 nor OS X v10.7 (Lion) IPython 0.11 suppressed. Although the flashing issue is worse in OS X, probably because of item2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonas
  • 145
  • 1
  • 4

3 Answers3

11

Try something like 1 + 1;. Without the semicolon, it should give you feedback about the result by printing it (formatted by repr, though it doesn't matter in the case of integers) - I assume that it's this output that's supposed to be suppressed.

The shell doesn't (and shouldn't) suppress writing to the file that happens to be referenced by sys.stdout (which is essentially what print does). This is an entirely different matter, and not the job of the shell.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Just to confirm, delnan is quite right about this - it's the result of an expression that you can suppress, not printing things. – Thomas K Sep 22 '11 at 20:57
  • Thank you for your fast and concise answer, though I am disappointed that it does not do what I hoped for I am thankful for clearing that up. – Jonas Sep 22 '11 at 23:21
6

Add %%capture as the first line of the cell. For example,

%%capture
print('Hello')

This simply discards the output, but the %%capture magic can be used to save the output to a variable—consult the documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zero
  • 11,593
  • 9
  • 52
  • 70
1

Here's another example from the Dataquest — 28 Jupyter Notebook tips, tricks, and shortcuts post:

  # Use a semicolon to suppress the output of a final function.
  %matplotlib inline
  from matplotlib import pyplot as plt
  import numpy
  x = numpy.linspace(0, 1, 1000)**1.5
  plt.hist(x); # Output not suppressed w/ semicolon?

And an example of a "working" semicolon suppression:

x = 1 + 1
x; # Output suppressed with semicolon!

So it appears to suppress for statements that would normally show up in the terminal, but not "inline" types, like plots.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pizoelectric
  • 422
  • 3
  • 10