How to Comment Out Multiple Lines in Python: Formatting & Shortcuts
How to Comment Out Multiple Lines in Python: Formatting & Shortcuts
Trying to comment out a block of code in Python? There are multiple easy methods to do so! You can use the hash character # or turn the lines into a string. The keyboard shortcut for turning lines into code varies depending on the text editor you’re using. This wikiHow shows you how to comment out multiple lines in Python.
Things You Should Know
  • Place a # and a space before each line of code.
  • Alternatively, place """ before and after the block of code.
  • To use a keyboard shortcut, select the block of code, then press the key combination.

Using the Hash Character

Type a # and a space before each line. The hash character is used in Python for single-line and multiple-line comments. Python is a great programming language for beginners and experienced programmers. Comments are an essential part of learning to program in Python.

Using a Multiple-Line String

Type """ before and after the lines. This turns the lines into a multiple-line string called a docstring. When a string isn’t assigned to a variable, Python will skip it when running the code. You can also use three single-quotes instead ('''). Docstrings are typically used for programming documentation and code summaries. Python’s style guide recommends using the hash character (#) for multi-line block comments. If you’re just getting started with Python, check out how to turn integers into strings. EXPERT TIP Kevin Burnett Kevin Burnett Software Developer Kevin Burnett is a Software Developer with over 20 years of professional experience. He spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Ruby, Python, and JavaScript. Kevin Burnett Kevin Burnett Software Developer Python commenting scales strategies. For commenting Python, use # symbols and triple quotes. # contains small chunks well, while """ """ wraps multiline snippets cleanly. The former scales better as comments lengthen, improving readability.

Keyboard Shortcuts

Use a keyboard shortcut. Many text editors include a keyboard shortcut for commenting out multiple lines of code. Select the code and press the shortcut to turn the selected lines into comments. Here are shortcuts for common text editors: Visual Studio: Press Ctrl+K then Ctrl+C Spyder IDE: Ctrl+1 IDLE: Alt+4 Jupyter Notebook: Ctrl+/ PyCharm: Ctrl+⇧ Shift+/

What's your reaction?

Comments

https://ugara.net/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!