562

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

6
  • 3
    This question was answered previously in Stack Overflow question Why doesn't Python have multiline comments?. Commented Mar 23, 2009 at 22:26
  • Additional guidelines of professional practice, "Don't use triple-quotes", distinguishes it from other posts ... Commented Jan 7, 2015 at 22:23
  • 14
    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. Commented Apr 8, 2019 at 15:53
  • 4
    Ctrl + / works for PyCharm Commented Apr 24, 2020 at 8:23
  • 1
    <snark>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> Commented Apr 29, 2020 at 20:10

19 Answers 19

489

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.

Sign up to request clarification or add additional context in comments.

23 Comments

For non-Americans, that's a "hash" sign.
in Notepad++ that's Ctrl+K (v.5.9.2) for any supported language
Even for Americans, "pound" should be £ or ₤.
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.
That is not technically correct. The symbol came from Roman "libra pondo", which translates as "pound weight" so it was called a "pound" symbol. In "An Elementary Treatise on Book-keeping by Single and Double Entry" by Samuel Worchester Crittenden written in 1853 he noted it as a "Number" symbol. The term "octothorp" was created by Bell Labs in the 1960's when the telephone engineers wanted to give a name to the symbol over the "8" on a phone's dial.
The creator of python actually suggests to use multi-line strings as block comments, so I would say your statement "Don't use triple-quotes" isn't appropriate.
|
107

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

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

or:

if False: '''
...statements...
'''

4 Comments

I don't think that is a good advice, you are adding complexity to your code without any real benefit. Someone reading that would have to figure out why is that code there and what is it suppouse to do.
What if the code you want to comment out already contains triple-quoted strings?
luckily for me it did not.
@keithThompson then use the other kind of triple quoted string
99

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

2 Comments

Clearly, all Real Python Programmers use ed, where this problem is easily solved with: 12,31s/^/#/
vim with nerdcommenter. Select the block you want and ,c<space>
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.

5 Comments

The op mentioned that they do not want the comments to appear as doc strings.
-1 retracted. That's a clever idea, though it may mean that the comments need comments :)
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.
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.
Ctrl+ / or Ctrl + Shift+/ in PyCharm does the same
45

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

4 Comments

This also works for PyCharm Community Edition, which is free and open-sourced.
CTRL+/ on Windows doesn't work for a Swedish keyboard layout.
Also works for VSCode on macOS
Also works on VSCode on Windows
29

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

1 Comment

M-; (comment-dwim) too
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.

Comments

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!^!#!

1 Comment

Or: CTRL+V (and select lines) => :s/^/#/g If text highlighting is bothering you => :noh
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.

1 Comment

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

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.

3 Comments

This works for Visual Studio Code as well.
On Windows for VS Code ctrl + /
I found out by clicking on 'Edit', then 'Toggle Line Comment'
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!

Comments

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.

Comments

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

1 Comment

but it is not pep8 format.
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.

Comments

2

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

2 Comments

to uncomment a block, use ctrl+shift+#
This also works in komodo-edit for python
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.

1 Comment

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

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.