5

In Jupyter Notebook , i can see that multilines commenting with triples quotes such as:

''' multi
lines
commented
out'''

will be printed in the [out] cell.

Is there a way to prevent that from happening? (ie not having the commented out lines printed in the output?

jim jarnac
  • 4,804
  • 11
  • 51
  • 88
  • That should only be printed if it's the only statement in the cell, if you have additional code following this then it should not be seen in the output – EdChum Jan 05 '17 at 09:45
  • 1
    Comment them out using the comment marker `#`? Triple quotes create strings not comments. – snakecharmerb Jan 05 '17 at 09:48
  • @ EdChum if i have code, and then the commented out lines they do show – jim jarnac Jan 05 '17 at 09:49
  • Because IPython will insert a print statement before the string I guess. – Mohammad Yusuf Jan 05 '17 at 10:01
  • I really don't see in what circumstance having a string or a commented out such as that printed would be useful – jim jarnac Jan 05 '17 at 10:03
  • Can you include the complete code where this is happening? – Mohammad Yusuf Jan 05 '17 at 10:03
  • 2
    It's not inserting a `print` statement; jupyter notebook prints the last value calculated in each cell. If you end a cell with a string literal, it will echo that on the [out]. – wildwilhelm Jan 05 '17 at 10:04
  • @wildwilhelm ok so if i want to comment out the bottom of the cell for debug purpose i will always have a slew of letters in the output, there is no way around that? – jim jarnac Jan 05 '17 at 10:06
  • 2
    Your fundamental misunderstanding is that a triple quote is not a comment, it is a string value, and like any value in a notebook will be printed out. – juanpa.arrivillaga Jan 05 '17 at 10:10

1 Answers1

14

As mentioned by @snakecharmerb, you can comment out the lines (with #) instead of wrapping them in a string. There is a keyboard shortcut for this: select the code that you want to comment (or uncomment, it's a toggle), and press Ctrl+/.

Alternatively, you can put a semicolon (;) after your close-quote to suppress the output of the string literal.

Community
  • 1
  • 1
wildwilhelm
  • 4,809
  • 1
  • 19
  • 24