553

Is there a mechanism to comment out large blocks of Python code?

Right now, the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in triple quotes: """.

The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation.

After reading all comments, the answer seems to be "No".

codeforester
  • 39,467
  • 16
  • 112
  • 140
gbarry
  • 10,352
  • 6
  • 33
  • 43
  • 3
    This question was answered previously in Stack Overflow question *[Why doesn't Python have multiline comments?](http://stackoverflow.com/questions/397148)*. – ChristopheD Mar 23 '09 at 22:26
  • Additional guidelines of professional practice, "Don't use triple-quotes", distinguishes it from other posts ... – AjayKumarBasuthkar Jan 07 '15 at 22:23
  • 13
    Sigh. One more useful and non-duplicate question marked as duplicate... This one asks for a solution, while the other one takes the answer (namely that no, there's no solution) as a prerequisite for asking what it has to ask. – Helen Apr 08 '19 at 15:53
  • 3
    Ctrl + / works for PyCharm – Lakhwinder Singh Dhillon Apr 24 '20 at 08:23
  • 1
    Perl allows you to use the documentation syntax for block commenting in such a way that it does NOT end up in the documentation. That's why we have more than one way to do things. It's called 'flexibility'. <\snark> – mpersico Apr 29 '20 at 20:10
  • For Jupyter, to comment use `Crtl + /` and uncomment use `Crtl - /` – circassia_ai Feb 15 '21 at 13:55

19 Answers19

481

Python does not have such a mechanism. Prepend a # to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, it's Alt+3 and Alt+4.

Don't use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If you're just commenting things out temporarily, this is fine as a temporary measure.

NerdOnTour
  • 634
  • 4
  • 15
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 1
    Geany also has this. Right click, Format, Comment Line(s). – NoBugs Oct 27 '11 at 02:06
  • 99
    For non-Americans, that's a "hash" sign. – edam Oct 31 '11 at 14:36
  • 75
    in **Notepad++** that's `Ctrl+K` (v.5.9.2) for any supported language – Janusz Lenar Dec 03 '11 at 00:50
  • 1
    In Eclipse, toggling comment for a selected block of code is performed using Ctrl+/. You do not need to select the code if it is only a single line. – lightalchemist Jun 06 '12 at 06:58
  • 51
    Even for Americans, "pound" should be £ or ₤. – glglgl Jul 04 '12 at 16:12
  • one disadvantage of mix multi-line string and block comments is IDE has no idea what you want thus can't show comment in different style as needed. – Baiyan Huang Sep 17 '12 at 03:58
  • 1
    In **Vim** with [NERDCommentor](http://www.vim.org/scripts/script.php?script_id=1218) installed, it is as easy as selecting the portion of code to be commented and pressing `\c` – Santosh Kumar Sep 29 '12 at 15:44
  • In PyCharm, it's CTRL-/ – sparc_spread Feb 20 '13 at 16:38
  • You can't triple-quote code that contains triple-quotes. – Keith Thompson Jun 26 '13 at 22:18
  • 70
    Actually, that symbol is called an octothorp. Please stop using local slang terms - few Americans call it a hash, and few non-Americans call it a pound, but nobody ever refers to anything else when they say octothorp. Except the person who chooses to defy this definitive answer by using it to mean something else. – ArtOfWarfare Jul 23 '13 at 14:33
  • 10
    The creator of python actually [suggests to use multi-line strings as block comments](http://stackoverflow.com/a/7696966/346561), so I would say your statement "Don't use triple-quotes" isn't appropriate. – Jesse Webb Oct 30 '13 at 19:19
  • 3
    @JesseWebb: He wrote that about two and a half years after this answer, though, so he changed his mind. :) – John Feminella Nov 01 '13 at 15:05
  • 9
    From a Microsoft developer, # is a "sharp" sign – BufBills Dec 20 '13 at 22:04
  • 2
    @JesseWebb Although it might be noted to use triple quotes, I found a bug in python library I was using (Pyshark) with regards to Python 3. there used to be this line in a triple quote comment in the pyshark library: (i.e. \Device\NPF_..). This was ignored in Python2 and ran fine, but Python 3 threw this error: "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 14-15: malformed \N character." Since then, I avoid triple quotes. – continuousqa Sep 23 '14 at 17:39
  • 3
    `Ctrl+/` in **PyCharm** will do comment / uncomment the highlighted rows – shreyansp Sep 11 '15 at 08:06
  • As said above, Python's creator actually suggests using triple quotes as block comments. Most editors will detect a triple-quoted string used in this way (not assigned or used in any way) and color it with the comment color. You can use `'''` to comment blocks of code containing `"""` and vice-versa. – Tobia Mar 23 '17 at 12:11
  • 7
    Calling it a hash, hash symbol, or number sign is the best bet. Especially now in 2017 hash is one of the most common ways to refer to that symbol. Nobody refers to it as an octothorpe except people trying to show off that they know that it's called an octothorpe. https://www.oxfordlearnersdictionaries.com/us/definition/english/hash_1?q=hash – Caboose Nov 01 '17 at 20:59
  • 3
    It's a sharp sign for musicians.(e.g A major scale: A B C# D E F# G# A) – Nirvan Nov 18 '17 at 20:55
  • 4
    @johnktejik This '♯' is a sharp sign. '#' this is the number sig. – szpanczyk Mar 13 '18 at 12:38
  • 5
    The correct term is 'number sign' (Unicode U+0023). It is also indexed under 'octothorpe', 'crosshatch', 'pound sign', and 'hash'. – Urhixidur May 09 '18 at 13:33
  • 2
    `few Americans call it a hash, and few non-Americans call it a pound` The number of Americans that call it a hash plus the number of non-Americans that call it a pound is at least 1,000 times the number of people on Earth that call it octothorpe. – Joe Oct 15 '21 at 14:24
  • And here this whole time I thought it was called a comment-symbol – emery Oct 18 '21 at 17:07
101

Hide the triple quotes in a context that won't be mistaken for a docstring, eg:

'''
...statements...
''' and None

or:

if False: '''
...statements...
'''
bobince
  • 528,062
  • 107
  • 651
  • 834
99

The only cure I know for this is a good editor. Sorry.

canen
  • 1,590
  • 9
  • 6
48

The only way you can do this without triple quotes is to add an:

if False:

And then indent all your code. Note that the code will still need to have proper syntax.


Many Python IDEs can add # for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 1
    The op mentioned that they do not want the comments to appear as doc strings. – Ed S. Mar 23 '09 at 22:22
  • -1 retracted. That's a clever idea, though it may mean that the comments need comments :) – Ed S. Mar 23 '09 at 22:49
  • 3
    That solution is similar to just commenting out the code, except that you add four spaces instead of # and that you also need to add "if False:" line. – Yoo Sep 23 '09 at 08:21
  • 1
    I was doing some script hacking and that is what I came up with. (So, +1). It's very slick that I can simply write "if False:", push the block over 1 tab and I'm done. I've used more than one editor where the method is nothing more than,highlight the block, then press tab. Strangely enough, I asked the original question for a friend, wanting to show off S.O. back when it was new. – gbarry Mar 31 '18 at 04:55
  • 2
    Ctrl+ / or Ctrl + Shift+/ in PyCharm does the same – Kazem Mar 24 '19 at 02:58
43

In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.

Mark Richman
  • 28,948
  • 25
  • 99
  • 159
marcinj
  • 48,511
  • 9
  • 79
  • 100
29

M-x comment-region, in Emacs' Python mode.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe W.
  • 1,442
  • 14
  • 9
18

At least in VIM you can select the first column of text you want to insert using Block Visual mode (CTRL+V in non-windows VIMs) and then prepend a # before each line using this sequence:

I#<esc>

In Block Visual mode I moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
13

In vi:

  • Go to top of block and mark it with letter a.
  • Go to bottom of block and mark it with letter b

Then do

:'a,'b s!^!#!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerry
  • 131
  • 1
  • 2
11
comm='''
Junk, or working code 
that I need to comment.
'''

You can replace comm by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx, oo, null, nil.

Harry
  • 3,684
  • 6
  • 39
  • 48
  • 1
    This would be loaded to memory at run time, and if the intention is to create a comment you want the program to ignore it. Leading every line with a `#` would be better. Also, don't assign things to a variable called `null`, that's just asking for disaster. – James Geddes Jan 02 '21 at 23:52
6

Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):

  • Ctrl + 4 - comment selected block

  • Ctrl + 5 - uncomment selected block

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
jacanterbury
  • 1,435
  • 1
  • 26
  • 36
6

In Eclipse + PyDev, Python block commenting is similar to Eclipse Java block commenting; select the lines you want to comment and use Ctrl + / to comment. To uncomment a commented block, do the same thing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arun
  • 10,685
  • 6
  • 59
  • 81
6

In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.

Thorkil Holm-Jacobsen
  • 7,287
  • 5
  • 30
  • 43
6

I use Notepad++ on a Windows machine, select your code, type CTRL-K. To uncomment you select code and press Ctrl + Shift + K.

Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it's free as in speech and as in beer!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dan
  • 61
  • 1
  • 1
4

The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.

As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaime Soriano
  • 7,309
  • 2
  • 33
  • 45
2

In Eclipse using PyDev, you can select a code block and press Ctrl + #.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamid
  • 21
  • 1
1

Triple quotes are OK to me. You can use ''' foo ''' for docstrings and """ bar """ for comments or vice-versa to make the code more readable.

Anonymous
  • 3,011
  • 4
  • 24
  • 23
  • My problem with triple quotes is that they actually are being checked for syntax. that has to be overhead that is unneeded for a comment. Case in point: if you had ''' /NPF ''' and run that in Python 3, it will throw a syntax error. So Python 3 is checking each triple quote for syntax validity. If you switch to # and comment the line, it is skipped. – continuousqa Sep 23 '14 at 18:38
1

On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.

deadly
  • 1,194
  • 14
  • 24
Evaldo
  • 11
  • 1
1

Another editor-based solution: text "rectangles" in Emacs.

Highlight the code you want to comment out, then C-x-r-t #

To un-comment the code: highlight, then C-x-r-k

I use this all-day, every day. (Assigned to hot-keys, of course.)

This and powerful regex search/replace is the reason I tolerate Emacs's other "eccentricities".

JS.
  • 14,781
  • 13
  • 63
  • 75
0

Use a nice editor like SciTe, select your code, press Ctrl + Q and done.

If you don't have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to 'effectively' comment it out. It is not the best practice though.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christian Witts
  • 11,375
  • 1
  • 33
  • 46