27

In Jupyter Notebook, you can toggle/comment a block of selected code with Ctrl + /, but this doesn't work in Google Colaboratory notebooks. Is there a way to comment out several lines of Python code easily in a Colaboratory notebook?

I've looked in the documentation, but I came up short.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Levon
  • 138,105
  • 33
  • 200
  • 191

8 Answers8

39

In the default Colab keyboard shortcuts, you can block comment text using Ctrl+/.

If this does not work for you, check Tools->Keyboard Shortcuts and search for "Comment Current Line" to see what the shortcut is within your current settings.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
jakevdp
  • 77,104
  • 11
  • 125
  • 160
8

You can select the lines of code and press (Ctrl + /) to comment or uncomment your selected lines of code.

You can also use triple single quotes (''') at the start and end of the code block you are interested to comment out.

''' ...Your commented code... '''

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TanD
  • 81
  • 1
  • I think your second solution is not correct. the three quotes it is for multiline string, not for comment. look this [notebook](https://colab.research.google.com/github/C4M-UofT/C4M-UofT.github.io/blob/master/notebooks/strings.ipynb#scrollTo=9Z2LNamTtG4W) – zana saedpanah Jun 07 '21 at 09:03
2

If you are a Mac user, you can use Command + / for commenting out, and if you want to uncomment again, select the lines and then Command + /.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mjachowdhury
  • 85
  • 10
1

For Mac users who don't have an American keyboard like me: it seems like Colab thinks you are using an American keyboard for shortcuts, and so to make the '/' character you have to press the second button to the right of the 'm'. I'll leave an image to explain.

My keyboard looks like this:

My layout

As I said, I have to press the . key to make '/' as if I were using an American keyboard.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liuker
  • 11
  • 1
  • Are you sure it is not the ***next*** key, with "_"/"-" (underscore/dash)? [United States keyboard layout](https://en.wikipedia.org/wiki/British_and_American_keyboards#Windows_keyboards) – Peter Mortensen May 09 '22 at 20:34
0

For Mac users:

  • first select the lines that you want to comment
  • second push these buttons: Command + Ctrl + Shift + /

Especially in the Italian keyboard it would be: Command + Ctrl + Shift + 7 (because the "/" character is combined with button "7")

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Tavana
  • 358
  • 2
  • 5
0

For some reason, I need to do Ctrl + Shift + :.

Which technically is Ctrl + /.

(I use an AZERTYUIOP keyboard.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tirbo06
  • 655
  • 6
  • 11
0

if you come here with an Italian keyboard you should press: command + control + .

code_lover
  • 25
  • 5
-1

When using Mac the keyboard shortcut is +/.

Note that depending on your layout, / cannot be directly addressed and its combination is different when combined with . For example, on a German keyboard / is [Shift]+7 but the combination +/ is +[Shift]+ß.

When you are unsure about which combination is correct, use the inbuilt keyboard viewer which provides a live view of the meaning of keys while selecting modifiers.

Keyboard Viewer: default keyboard mapping

enter image description here

Keyboard Viewer: modifier keys keyboard mapping

enter image description here

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119