In many programming languages, we use symbols like /* */ to create block comments. But Python doesn’t seem to have a dedicated multiline comment syntax. So what’s the best or recommended way to write a python multiline comment—especially when documenting multiple lines of explanation or disabling chunks of code temporarily?
Honestly, Python doesn’t have a dedicated syntax for block comments like C or Java, where you use /* */
. But from what I’ve found over the years, the most straightforward approach is to just use multiple #
symbols for each line of your comment. Like this:
# This is line 1 of my comment
# This is line 2
# And so on...
It’s simple, clear, and the go-to method in almost all Python codebases I’ve worked with. You’ll often see this in code reviews too—super readable, and there’s no room for confusion. So yeah, that’s the most accepted way to handle a Python multiline comment.
Yeah, @sam.aarun 's right, but I do want to add something here. While the #
method is the standard, when you’re writing docstrings or documentation inside functions, classes, or modules, I’ve seen people use triple quotes ("""
) for multi-line explanations. However, these aren’t technically comments, they’re docstrings, and they only work as documentation if they’re placed as the first line in a function or class.
For example:
def process():
"""This function handles user login and token validation."""
pass
This works great for explaining the function’s purpose, but it’s a Python multiline comment only in the context of docstrings. Outside of that, it’s not really treated as a comment, more like a string that Python reads, so it’s something to be mindful of.
Right, @shilpa.chandel . I used to use triple quotes too when I was disabling chunks of code temporarily, like so:
"""
Temporarily disabling this block
for testing some logic.
"""
It worked for the most part, but turns out that even though the code inside the quotes isn’t executed, Python still treats it as a string, which gets loaded into memory. So, you might see linter warnings about unused string objects, and that can get a little annoying. Personally, I stick with the #
method for commenting out code now it’s cleaner, clearer, and no risk of loading unnecessary strings. For temporarily disabling chunks of code, I’d recommend commenting out each line, just to avoid any confusion. So, in the case of a Python multiline comment, the #
method is the safest bet.