How to comment out multiple lines of code in Python?

Dung Do Tien Aug 01 2021 74

Hello, I am a newbie in Python for a week. I know to command a line of code we can use #

But have some cases I want to comment out multiple lines of code in Python. I try with /**/

 /*
Hello, I'm a newbie in Python.
How can I comment out a block of code?
Thanks!!!
*/

print('Hello, world!')

But it does not work for me. How can I do it?

Have 3 answer(s) found.
  • m

    majid gholipour Aug 01 2021

    You can't use /**/ to comment out multiple lines of code. You can replace it with ''' '''. See an example below:

     '''
    Hello, I'm a newbie in Python.
    How can I comment out a block of code?
    Thanks!!!
    '''
    
    print('Hello, world!')

    hope it helpful for you.

  • D

    Developer Aug 01 2021

    You can use """ to help comment out a block of code in Python. See an example:

     def add_stuff(a, b):
        result = a + b
        """
        Now we return the result, wee!
        Hurray! I'm so excited I can't contain
        my joy to just one or two lines!
        """
        return result
    
    number = add_stuff(1, 2)
    print(number)

    It seems so easy!!!

  • U

    Unnop Niratiam Aug 01 2021

    1. In Visual Studio, blocks can be commented out by Ctrl+K+C and uncommented by Ctrl+K+U.

    2. In PyDev (and in Aptana Studio with PyDev):
      - Ctrl + 4 - comment selected block
      - Ctrl + 5 - uncomment a selected block

    3. In Eclipse using PyDev, you can select a code block and press Ctrl + #.

    4. On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.

    5. In JetBrains PyCharm on Mac use Command + / to comment/uncomment a selected block of code. On Windows, use CTRL + /.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close