'Is it possible to do block-comments in Google Colab?

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.



Solution 1:[1]

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.

Solution 2:[2]

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... '''

Solution 3:[3]

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 + /.

Solution 4:[4]

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

Solution 5:[5]

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.

Solution 6:[6]

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")

Solution 7:[7]

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

Which technically is Ctrl + /.

(I use an AZERTYUIOP keyboard.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Misha Akopov
Solution 2 Peter Mortensen
Solution 3 Peter Mortensen
Solution 4
Solution 5 Peter Mortensen
Solution 6 Peter Mortensen
Solution 7 Peter Mortensen