• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Comments / Shortcut to comment out multiple lines in Python

Shortcut to comment out multiple lines in Python

Author: Aditya Raj
Last Updated: April 13, 2021

We often need to comment out block of codes in python while testing or debugging the code. When a block is turned into a python comment, it doesn’t contribute in output of the program and helps to determine which function or block is generating error in the program. In this article, we will look at some shortcut to comment out multiple lines of code at once in different python IDEs. Lets see examples for each IDE one by one.

Shortcut to comment out multiple lines in Spyder

In spyder python IDE, we can comment a single line of code by selecting the line and then using the key combination ctrl+1 . This will turn the selected single line to a comment as shown below. The function given in the example adds a number and its square to a python dictionary as as key-value pair.

print("This line will be commented out.")
def add_square_to_dict(x,mydict):
    a=x*x
    mydict[str(x)]=a
    return mydict
    

After pressing ctrl+1:

#print("This line will be commented out.")
def add_square_to_dict(x,mydict):
    a=x*x
    mydict[str(x)]=a
    return mydict
    

The shortcut to comment out multiple lines of code in spyder IDE is to first select all the lines which need to be commented out and then the key combination ctrl+4 is pressed. This turns the entire selected lines into a python comment as shown below.


  class MyNumber():
      """This is the docstring of this class.
      
      It describes what this class does and all its attributes."""
      def __init__(self, value):
          self.value=value
      def increment(self):
          """This is the docstring for this method.
          
          It describes what the method does, what are its calling conventions and
          what are its side effects"""
          self.value=self.value+1
          return self.value
  print (MyNumber.increment.__doc__)

After pressing ctrl+4:

# =============================================================================
# 
#   class MyNumber():
#       """This is the docstring of this class.
#       
#       It describes what this class does and all its attributes."""
#       def __init__(self, value):
#           self.value=value
#       def increment(self):
#           """This is the docstring for this method.
#           
#           It describes what the method does, what are its calling conventions and
#           what are its side effects"""
#           self.value=self.value+1
#           return self.value
#   print (MyNumber.increment.__doc__)
# =============================================================================

We can also use ctrl+1 to uncomment the lines after selecting them when they are commented out or comment out the lines after selecting them to comment them out. In some versions of Spyder ctrl+5 can be used to uncomment the lines of code.

Shortcut to comment out multiple lines in IDLE

To comment out a block of code in IDLE, we have to first select the line and then press the key combination ctrl+D. This will comment out the selected lines of code as shown below.

 class MyNumber():
         """This is the docstring of this class.
         
         It describes what this class does and all its attributes."""
         def __init__(self, value):
             self.value=value
         def increment(self):
             """This is the docstring for this method.
             
             It describes what the method does, what are its calling conventions and
             what are its side effects"""
             self.value=self.value+1
             return self.value
     print (MyNumber.increment.__doc__)

After pressing ctrl+D:

## class MyNumber():
##         """This is the docstring of this class.
##         
##         It describes what this class does and all its attributes."""
##         def __init__(self, value):
##             self.value=value
##         def increment(self):
##             """This is the docstring for this method.
##             
##             It describes what the method does, what are its calling conventions and
##             what are its side effects"""
##             self.value=self.value+1
##             return self.value
##     print (MyNumber.increment.__doc__)

To uncomment the lines of code, we just have to select the lines and then press ctrl+shift+d. This will uncomment the selected lines.

Shortcut to comment out multiple lines in Jupyter Notebook

We can use ctrl+/ to comment out the selected lines of python code in Jupyter Notebook. This turns selected lines of code into comment as shown below.


     class MyNumber():
         """This is the docstring of this class.
         
         It describes what this class does and all its attributes."""
         def __init__(self, value):
             self.value=value
         def increment(self):
             """This is the docstring for this method.
             
             It describes what the method does, what are its calling conventions and
             what are its side effects"""
             self.value=self.value+1
             return self.value
     print (MyNumber.increment.__doc__)

After pressing ctrl+/:


#      class MyNumber():
#          """This is the docstring of this class.
         
#          It describes what this class does and all its attributes."""
#          def __init__(self, value):
#              self.value=value
#          def increment(self):
#              """This is the docstring for this method.
             
#              It describes what the method does, what are its calling conventions and
#              what are its side effects"""
#              self.value=self.value+1
#              return self.value
#      print (MyNumber.increment.__doc__)

To uncomment the selected lines, we just have to again press ctrl+/.

Comment out multiple lines in Pycharm

If we have to comment out multiple lines of code in Pycharm, we can select the lines to be commented out and then press ctrl+shift+/ . To uncomment the lines, we just have to select the lines and then again press ctrl+shift+/.

Conclusion

In this article, we have seen shortcuts to comment out multiple lines at once in python different IDEs like spyder, IDLE, Jupyter Notebook and PyCharm. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Comments Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Set Difference in Python
  • Convert List of Lists to CSV File in Python
  • Remove Elements From a Set in Python
  • Check if a Python String Contains a Number
  • How to Compare Two Files in Python Line by Line

Copyright © 2012–2022 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us