Skip to main navigation
#! code
Code Tips, Snippets, Resources, Tutorials And Help
  • Home
  • Tools
  • About
  • Contact

Multi-line Comments In Python

By philipnorton42 on 13th April 2012

Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:

  1. # This is a single line comment.

You can create a multi-line comment in Python by using a multi-line string. Unless it is part of a docstring at the start of a class, function or module then it is ignored.

  1. '''
  2. This is
  3. a multiline
  4. comment
  5. '''

Multi-line strings are used when you want to create a string that is split across several lines. If you create one and don't assign it to a variable then it is just thrown away. Multi-line strings can be used in the following way.

  1. multilinestring = '''
  2. This is
  3. a multiline
  4. string
  5. '''
  6. print multilinestring
Category
Python
Tags
python

Comments

Permalink

Anonymous - Fri, 12/30/2016 - 20:52

No, it is not ignored. It is parsed. see https://stackoverflow.com/questions/7696924/way-to-create-multiline-com…

Add new comment

The content of this field is kept private and will not be shown publicly.
About text formats
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
  • Add new comment

Categories

  • Ansible
  • Apache
  • Book Reviews
  • CSS
  • DOS/Windows
  • Drupal
    • Drupal 8
    • SimpleTest
  • Flex/Flash
  • General
  • Git
  • HTML/XHTML
  • JavaScript
    • JavaScript Strings
    • JavaScript Websites
    • JQuery
    • MooTools
    • OpenLayers
    • Script.aculo.us
  • Linux/Unix
  • MS SQL
  • OSX
  • PhoneGap
  • PHP
    • Phing
    • PHP Arrays
    • PHP Questions
    • PHP Strings
    • PHP Websites
    • Zend Framework
  • PostgreSQL
  • Python
  • Regular Expressions
  • Regular Expressions Websites
  • SQL
    • MS SQL
    • MySQL
    • PostgreSQL
  • Vagrant
  • Websites
  • WordPress

User login

  • Reset your password

© 2019 #! code

  • Twitter
  • Facebook
  • Google+
  • Github
  • RSS
  • Colophon
  • Privacy Policy
  • Terms and License