PyCharm

How to Comment in PyCharm for Efficiency

How to Comment in PyCharm for Efficiency

The shortcut is Ctrl+/ on Windows and Linux and Cmd+/ on macOS. Press it on a line, or on a selection, and PyCharm puts a # at the start of each line. Press it again and they’re gone.

PyCharm is JetBrains’ Python IDE, and it uses this one action both to disable code for a while and to leave notes in it.

One thing trips people up. JetBrains documents that block comments do not apply to Python scripts (PyCharm 2026.2 Help), so the Ctrl+Shift+/ shortcut that tutorials often cite for block comments is the wrong tool here. Python has no block comment syntax, which is why a selection gets one # per line.

How to Comment and Uncomment a Line in PyCharm

YouTube player

Put the caret on the line and press Ctrl+/ on Windows and Linux or Cmd+/ on macOS. A # lands at the start of the line.

Press the same keys again and it’s removed. There’s no separate uncomment key, which I’d call a small mercy, since it’s one less thing to memorize.

PlatformShortcutKeymap note
WindowsCtrl+/Default Windows keymap
LinuxCtrl+/Same default keymap card as Windows
macOSCmd+/macOS keymap

What PyCharm writes is the standard Python comment, a hash outside a string literal that runs to the end of the physical line and is ignored by the syntax, per the Python 3.14 Language Reference.

Python has no other comment token, so every commented line carries its own hash.

The toggle is one entry in a longer list of shortcuts built into PyCharm. Which key combination applies depends on the keymap selected in Settings, and PyCharm suggests the one that matches your operating system (JetBrains Help, PyCharm 2026.2).

Commenting Out Multiple Lines

Select the lines and use the same shortcut. Every selected line gets a #, and a second press strips them.

A Python comment ends where the physical line ends, so there’s no closing marker that could cover several lines at once.

Why is Python conquering the tech world?

Explore comprehensive Python statistics: market dominance, developer preferences, salary insights, and growth trends across industries.

Discover Python Insights →

Some people wrap the code in triple quotes instead. It looks like a block comment, but Python treats it as a string, and that causes trouble in a few ways.

Code that already contains triple double quotes closes the wrapper early, because three unescaped quotes of the same kind end the literal. If the wrapper sits as the first statement of a function body, it becomes that function’s docstring (PEP 257). And nothing toggles it back, so removing it means deleting both quote lines by hand.

MethodHow Python treats itToggle in PyCharm
Comment with Line CommentComment, ignored by the syntaxYes, the same shortcut reverses it
Triple-quoted stringString literal, or a docstring in first positionNo, wrap and unwrap by hand
Comment with Block CommentNot applicable to PythonNot supported for Python

Commenting lines out one by one to isolate a crash is slow. Stepping through the code in the PyCharm debugger finds the failing line faster and leaves the source untouched.

Commented-out code that outlives a debugging session should be deleted. Version control keeps every old line retrievable, so the file can stay readable.

My rule is to use the toggle for temporary disabling and delete anything permanent.

New to Python or just need a quick reference? List comprehensions, built-in functions, and core syntax - including lambda, dict methods, and file I/O - is on one page in the Python Cheat Sheet.

Does PyCharm Support Block Comments for Python?

No. JetBrains states in the PyCharm 2026.2 help that block comments do not apply to Python scripts.

The Comment with Block Comment action, bound to Ctrl+Shift+/ by default, belongs to languages that have a block comment syntax. Python doesn’t have one.

A JetBrains support reply on the community forum confirms it and points to YouTrack issue PY-6185, which tracks block comments in Python files. The stated reason is that Python has no block comments, and triple-quoted strings do not officially count as one.

The sources don’t agree on this, though. JetBrains documentation and support say the block comment action does not apply to Python, so Comment with Line Comment on the selection is the answer. Several tutorials list Ctrl+Shift+/ as the PyCharm block comment shortcut for selected lines and add no Python caveat (filipchabik.ghost.is).

The tutorials describe a shortcut that exists in the IDE. The documentation describes what it does inside a .py file. For Python, the documentation wins.

In a .py file, Ctrl+/ on a selection works. That’s Comment with Line Comment, and the same shortcut reverses it.

Ctrl+Shift+/ as a block comment doesn’t work, and neither does the Comment with Block Comment action under any other shortcut. One community poster who could not bind a custom key to it got the same “unsupported for Python” answer from JetBrains.

Ctrl+/ on the selection gives the same visual result, a # down the left edge of the block. The block action stays in the keymap for other languages.

How to Generate a Docstring in PyCharm

Type three double quotes under a function declaration and press Enter or Space. PyCharm fills in a stub listing the parameters in the docstring format chosen in Settings.

A string literal placed as the first statement of a module, function, class, or method becomes that object’s \_\doc\\_ attribute (PEP 257). That’s a docstring. It counts as documentation rather than a comment, and the interpreter keeps it.

Generate a stub with triple quotes

  1. Place the caret after the function declaration.
  2. Type the opening triple quotes and press Enter or Space.
  3. Describe each parameter and the return value in the generated stub.

Space only triggers the stub when the Insert pair quote checkbox is cleared on the Smart Keys page of the editor settings (JetBrains Help). Enter works either way. PEP 257 recommends triple double quotes even for one-line docstrings. A string literal further down the body is just a string, not a docstring.

Generate a stub with an intention action

Put the caret anywhere inside the function and press Alt+Enter. Pick Insert documentation string stub from the list.

Same stub, same format. It saves a scroll back up to the declaration in a long function.

Choose the docstring format

The stub style comes from the Docstring format dropdown, opened with Ctrl+Alt+S. The page moved between releases: the 2024.1 help places it at Tools | Python Integrated Tools, while the 2026.1 help places it at Python | Tools | Integrated Tools.

FormatStub on Enter or SpaceQuick Documentation rendering
PlainEmpty stubPlain text
reStructuredTextreStructuredText layoutDocutils
NumPyNumPy layoutNapoleon and Docutils
GoogleGoogle layoutNapoleon and Docutils

The choice shows up in the text itself. For a function with the parameters self, myParam1 and myParam2, reStructuredText writes :param lines for each parameter and a :return: line (JetBrains Help, PyCharm 2026.2). Older help versions such as 2024.1 also listed Epytext, which writes @param lines. The 2026.1 and 2026.2 pages no longer list it.

The format also controls how View | Quick Documentation renders the docstring when you hover over the function elsewhere in the project.

TODO Comments and the TODO Tool Window

Start a comment with TODO or FIXME and PyCharm highlights it and indexes it, then lists it in the TODO tool window. Open the window through View | Tool Windows | TODO.

Both patterns match in lower and upper case by default, and they apply inside line and block comments of any supported file type (JetBrains Help, PyCharm 2026.2). In a .py file that means a # comment.

The window has tabs for all files in the current project, the current file only, a chosen scope, and the active changelist once version control integration is set up.

Clicking an item jumps to the comment. A single click navigates by default, and a toolbar button switches that to a double click.

Custom patterns live under Settings | Editor | TODO and use regular expressions. JetBrains’ own example is \boptimize\b.\*, which marks optimization spots and can be grouped under a filter named Optimization.

Each pattern carries an icon that sets its items apart in the list. A Case Sensitive checkbox forces an exact-case match.

Multiline items work through indentation. Indent the comment lines after the first one with spaces, tabs, or a mix, and PyCharm treats them as one TODO. A checkbox on the same settings page turns that off.

Unresolved markers are a lightweight record of technical debt. The tool window makes that record countable per file, scope, or changelist.

Comment Style Settings in Code Style

JetBrains places comment alignment and spacing under the Comment Code options in Settings | Editor | Code Style. Two checkboxes shape a hash line, Line comment at first column and Add a space at line comment start.

With Line comment at first column selected, line comments start at column one. Clear it and they align with the code. The other checkbox inserts a space between the comment character and the first character of the commented text.

The sources don’t agree on where these options live. The general source-code page of the PyCharm 2026.2 help sends readers to the Comment Code section on the Code Generation tab of each language page. The Python code style page for the same release lists Tabs and Indents, Spaces, Wrapping and Braces, Blank lines, Imports, and Other, with no Code Generation tab.

The checkbox descriptions above come from JetBrains’ reference page for the Less language. Open Settings | Editor | Code Style | Python and check whether the tab exists before building a team convention on it.

PEP 8 (last revised April 2025) has a few numbers worth knowing.

  • 1 space after the # in block and inline comments
  • 2 spaces at minimum between a statement and its inline comment
  • 72 characters maximum for comment and docstring lines
  • 79 characters maximum for code lines

Teams that raise the code limit to 99 characters still wrap comments at 72. PEP 8 also asks for sparing inline comments, none that restate the obvious, and warns that a comment contradicting the code is worse than no comment.

Selecting the space option matches the single space PEP 8 expects after the hash.

Keeping the editor aligned with the style guide also stops it from disagreeing with a linter that enforces PEP 8.

Changing the Comment Shortcut and Fixing Conflicts

Reassign it under Keymap in Settings. Find Comment with Line Comment, right-click it, and add a keyboard shortcut. When the default keys fail on your keyboard layout, you can also remap the action or run it from the Code menu.

Run the action from the Code menu or Find Action

JetBrains community posts give the menu path as Code | Comment with Line Comment. Select the lines first, then pick the entry.

Find Action reaches the same command by name. Press Ctrl+Shift+A, type the action name, and press Enter (JetBrains Help).

Both routes work on any layout, but every toggle costs extra clicks or keystrokes.

Reassign the shortcut in Keymap settings

  1. Press Ctrl+Alt+S and open Keymap.
  2. Type Comment with Line Comment in the search field.
  3. Right-click the action and select Add Keyboard Shortcut.
  4. Press the new key combination in the dialog. PyCharm warns when it clashes with an existing shortcut.
  5. Click OK.

Predefined keymaps are read-only. On the first change PyCharm saves a copy that stores only the differences from its parent keymap, so the custom file stays small and moves between installations.

Right-click the action and choose Reset Shortcuts to undo the change. Shortcuts cannot start with the Fn key.

Fix layout and system shortcut conflicts

JetBrains states that on a keyboard without an English layout, PyCharm may not detect every shortcut correctly. Community posts show how that plays out.

  • On an Italian layout the slash sits behind Shift+7, which is why one community poster went looking for a custom binding.
  • A Danish layout on a MacBook left Cmd+/ doing nothing. A JetBrains reply advised remapping the action to a combination that works, or reverting to the older macOS behavior described in a support article.
  • Global OS shortcuts can clash too. PyCharm notifies you about it, and the macOS System Shortcuts keymap exists to avoid them.

If the layout has no direct slash key, remap the action. A one-time binding beats a menu trip on every toggle.

Commenting in PyCharm FAQ

Is there a difference between PyCharm Community and Professional for commenting?

No edition difference is documented. JetBrains’ shortcut reference lists the comment actions once, with no edition split.

Since PyCharm 2025.1 the two editions have been merged into one unified PyCharm with free core features, and Community 2025.2 was the last standalone release (JetBrains Help).

What is the difference between a comment and a docstring in Python?

Python drops a comment when it parses the file. A docstring stays on the object as its \_\doc\\_ attribute.

PEP 8 asks for docstrings on public modules, functions, classes, and methods, and for a comment under the def line of non-public methods.

Can the comment shortcut be used in files that are not Python?

Yes, in files whose language PyCharm recognizes, because the action follows that language’s comment syntax. JetBrains keeps its comment settings on per-language Code Style pages for the same reason.

TODO markers follow the same rule, and JetBrains says to write them in the standard comment style of the language you are working in.

Matching Each Comment Type to Its Job

Each kind of comment has its own tool. A # line disables code or records a decision. Docstrings describe a function to the people calling it, and TODO markers flag work that’s still open.

I’d learn them in order of how easily they undo. The line toggle reverses in one keystroke, docstrings live on the object, and TODO markers persist in the project index.

The cost is upkeep. Every marker left in a file is a line someone has to maintain, so delete a TODO the moment its work ships.

This order holds for PyCharm 2026.2. Block comment support for Python would change it. JetBrains’ help excludes it today, YouTrack issue PY-6185 tracks the request, and the position was verified on 7 October 2026.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.