How to comment group of lines in python
Commenting is an integral part of every programming language. With comments, you get a better
understanding of your own code, make it more readable, and can help team members understand how it works. Comments are ignored by compilers and interpreters, so they don’t run. Apart from making your code more readable, comments can also help while you're debugging – if you have two lines of code, you can comment out one to prevent it from running. Just like other programming languages, Python supports comments. The problem is that Python doesn't have a built-in mechanism
for multi-line comments. So in this article, I won't just show you how to make single-line comments in Python – I'll also show you the workaround for making multi-line comments. To make single-line comments in Python, prepend each line with a hash ( Output: As you can see, the commented line wasn't printed in the output. Unlike other programming languages such as JavaScript, Java, and C++ which use To comment out multiple lines in Python, you can prepend each line with a hash ( Output: With this approach, you're technically making multiple single-line comments. The real workaround for making multi-line comments in Python is by using docstrings. If you use a docstring to comment out multiple line of code in Python, that block of code will be ignored, and only the
lines outside the docstring will run. Output: NB: One thing to note is that while using doctsrings for commenting, indentation still matters. If you use 4 spaces (or a tab) for indentation, you will get an indentation error. For example, this will work: But this won't work: So your IDE will throw the error " Since there's no built-in support for multi-line comments in
Python, this article demonstrates how you can use docstrings as a workaround. Still, you should generally stick to using regular Python comments using a hash ( If you found this article helpful, consider sharing it with your friends and family. Thank you for reading. Learn to code for free. freeCodeCamp's open
source curriculum has helped more than 40,000 people get jobs as developers. Get started You can use triple-quoted strings. When they're not a docstring (the first thing in a class/function/module), they are ignored.
(Make sure to indent the leading Guido van Rossum (creator of Python) tweeted this as a "pro tip". However, Python's style guide, PEP8, favors using consecutive single-line comments, like this:
...and this is also what you'll find in many projects. Text editors usually have a shortcut to do this easily.
larsks 241k37 gold badges358 silver badges348 bronze badges answered Oct 8, 2011 at 12:58
Petr ViktorinPetr Viktorin 63.7k9 gold badges80 silver badges80 bronze badges 28 Python does have a multiline string/comment syntax in the sense that unless used as docstrings,
multiline strings generate no bytecode -- just like On the other hand, if you say this behavior must be documented in the official documentation to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification. In any case, your text editor
should also be able to easily comment-out a selected region (by placing a Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived. Not only should the text editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and it should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful. To protect against link decay, here is the content of Guido van Rossum's tweet:
answered Oct 8, 2011 at 12:54
unutbuunutbu 797k171 gold badges1721 silver badges1623 bronze badges 10 From the accepted answer...
This is simply not true. Unlike comments, triple-quoted strings are still parsed and must be syntactically valid, regardless of where they appear in the source code. If you try to run this code...
You'll get either...
...on Python 2.x or...
...on Python 3.x. The only way to do multi-line comments which are ignored by the parser is...
answered Jun 29, 2016 at 13:04
4 In Python 2.7 the multiline comment is:
In case you are inside a class you should tab it properly. For example:
ncica 6,8821 gold badge14 silver badges33 bronze badges answered Feb 16, 2015 at 14:00
3 I think it doesn't, except that a multiline string isn't processed. However, most, if not all Python IDEs have a shortkey for 'commenting out' multiple lines of code.
answered Oct 8, 2011 at 12:54
Anti EarthAnti Earth 4,55311 gold badges51 silver badges82 bronze badges There is no such feature as a multi-line comment. It seems to work, but internally Check the official documentation here
answered Oct 6, 2018 at 12:09
RATAN KUMARRATAN KUMAR 5046 silver badges11 bronze badges 1 If you put a comment in
in the middle of a script, Python/linters won't recognize that. Folding will be messed up, as the above comment is not part of the standard recommendations. It's better to use
If you use Vim, you can plugins like commentary.vim, to automatically comment out long lines of comments
by pressing If you don’t want to use plugins like the above you can use search and replace like
This will replace the first character on the current and next line with
answered Jan 31, 2018 at 18:16
alpha_989alpha_989 4,5511 gold badge34 silver badges47 bronze badges 0 Visual Studio Code universal official multi-line comment toggle. Similar to Xcode shortcut. macOS: Select code-block and then ⌘+/ Windows: Select code-block and then Ctrl+/ answered Aug 29, 2019 at 4:01
EdisonEdison 11.6k4 gold badges39 silver badges49 bronze badges Unfortunately stringification can not
always be used as commenting out! So it is safer to stick to the standard prepending each line with a Here is an example:
answered Jun 27, 2018 at 14:44
I would advise against using Here is a simple example to highlight what might be considered an unexpected behavior:
Now have a look at the output:
The multi line string was not treated as comment, but it was concatenated with If you want to comment multiple lines do so according to PEP 8 guidelines:
Output:
answered Sep 29, 2019 at 17:38
jojojojo 9,4482 gold badges48 silver badges69 bronze badges 0 Well, you can try this (when running the quoted, the input to the first question should quoted with
Whatever enclosed between If you are looking for single-line comments then it's
answered Sep 15, 2017 at 5:27
K_hollaK_holla 1241 silver badge3 bronze badges Multiline comment in Python: For me, both ''' and """ worked. Example:
Example:
answered Aug 30, 2018 at 8:41
Viraj WadateViraj Wadate 4,5311 gold badge27 silver badges28 bronze badges On Python 2.7.13: Single:
Multiline:
answered May 11, 2017 at 15:47
Alvin GeorgeAlvin George 13.8k87 silver badges64 bronze badges 2 The inline comments in Python starts with a hash character.
Note that a hash character within a string literal is just a hash character.
A hash character can also be used for single or multiple lines comments.
Enclose the text with triple double quotes to support docstring.
Enclose the text with triple single quotes for block comments.
answered Dec 15, 2018 at 4:26
Madan SapkotaMadan Sapkota 23.8k11 gold badges112 silver badges115 bronze badges If you write a comment in a line with a code, you must write a comment, leaving 2 spaces before the # sign and 1 space before the # sign
If you write a comment on a new line, you must write a comment, leaving 1 space kn in the # sign
To write comments longer than 1 line, you use 3 quotes
Georgy 10.8k7 gold badges62 silver badges68 bronze badges answered Sep 8, 2020 at 15:44
1 Using PyCharm IDE.
Select all lines then press Ctrl + /
answered Mar 14, 2019 at 7:53
0m3r0m3r 11.8k15 gold badges31 silver badges68 bronze badges Yes, it is fine to use both:
and
But, the only thing you all need to remember while running in an IDE, is you have to 'RUN' the entire file to be accepted as multiple lines codes. Line by line 'RUN' won't work properly and will show an error.
answered Jun 11, 2019 at 12:41
Among other answers, I find the easiest way is to use the IDE comment
functions which use the Python comment support of I am using Anaconda Spyder and it has:
It would comment/uncomment a single/multi line/s of code with I find it the easiest. For example, a block comment:
answered Nov 7, 2019 at 14:32
aniltilantheaniltilanthe 3,8071 gold badge15 silver badges17 bronze badges 0 This can be done in Vim text editor. Go to the beginning of the first line in the comment area. Press Ctrl+V to enter the visual mode. Use arrow keys to select all the lines to be commented. Press Shift+I. Press # (or Shift+3). Press Esc. answered Aug 21 at 4:07
Liker777Liker777 2,1334 gold badges17 silver badges24 bronze badges For commenting out multiple lines of code in Python is to simply use a
For writing “proper” multi-line comments in Python is to use multi-line strings with the
Also, mention that you can access docstring by a class object like this
answered Jul 8, 2019 at 3:34
shafikshafik 5,7075 gold badges34 silver badges45 bronze badges 2 A multiline comment doesn't actually exist in Python. The below example consists of an unassigned string, which is validated by Python for syntactical errors. A few text editors, like Notepad++, provide us shortcuts to comment out a written piece of code or words.
Also, Ctrl + K is a shortcut in Notepad++ to block comment. It adds a
answered Dec 10, 2018 at 9:20
0 Select the lines that you want to comment and then use Ctrl + ? to comment or uncomment the Python code in the Sublime Text editor. For single line you can use Shift + #.
answered Jan 22, 2019 at 11:04
You can use the following. This is called DockString.
answered Jan 21, 2020 at 18:09
unknownunknown 3245 silver badges24 bronze badges 2 Yes, you can simply use
or
BONUS: It's a little bit harder, but it's safer to use in older versions,
For this one, you can select the text you want to comment and press But you can edit them. For example, you can change the shortcut from WARNING!
Hope this answer helped. Good luck next time when you'll write other answers! answered Jun 16 at 11:03
in windows: you can also select the text or code chunks and press answered Aug 13 at 13:32
Seyma KalaySeyma Kalay 1,7939 silver badges15 bronze badges I read about all of the drawbacks of the various ways of doing this, and I came up with this way, in an attempt to check all the boxes:
Pros
Cons
Here's an attempt at the VS Code macro, although I haven't tested it yet:
answered Sep 23 at 15:57
toddmotoddmo 19k13 gold badges89 silver badges100 bronze badges How do you comment multiple lines in Python?To comment out multiple lines in Python, you can prepend each line with a hash ( # ). With this approach, you're technically making multiple single-line comments.
How do you comment a group of lines?Press Ctrl + /. Select all the lines that you would like to be commented.. Press Ctrl + / Two slashes "//" will be added to the front of each line, causing them to be recognized as a comment.. How do you comment out multiple lines in a notebook in Python?All you need to do, is select all the lines you want to comment and press Ctrl + / as shown in below video.
How do you comment multiple lines in Python Ctrl?Comment with Line Comment. the first press Ctrl + / will comment all lines (adding the second comment symbol # # in front of the commented lines). the second one Ctrl + / will uncomment all lines (only the first comment sign). |