Comments in Python Programming


As all other programming languages, Python do supports multiple ways of adding comments to your code,

Comments in Python start with a # (hash) sign, all the lines in your code that start with # are ignored.

How to add a single line comment in Python

Example:
#This is my python code comment line.
print("Welcome!")

How to place comment at the end of the line in Python?

Example:
print("Pyhton comments!") #This comment will be ignored

How to comment out code in Python?

You can simply add a # (hash) at the start of each statement to comment it out.

Example:
#print("I dont want this code")
print("I want this code")

How to add multi-line comment in Python?

Like Java we do not have a different syntax for muli-line comments in Python, one way is to add # at the start of each line.

Example:
# This is
# a multiline
# comment in 
# Python
print("Hello there!")

Another way of adding multiline comments is to enclose your comment with """ (3 double quotes - triple quotes)

Example:
"""
This is
a multiline
comment in 
Python
"""
print("Hello there!")

Comments:

  • My Python comments were not working, realized i was using slash instead of hash :D, thank you!
    user: anon 02 Oct 2020 11:01:11 GMT
  • Python comments block is what I was looking for.
    user: anon 02 Oct 2020 01:15:25 GMT
  • How to convert python comments to markdown class documentation?
    user: anon 01 Oct 2020 13:19:59 GMT
  • " python comments are useful and recommended!
    user: anon 28 Aug 2020 15:11:14 GMT
  • /** is not Pyhton style of comments.
    user: anon 27 Aug 2020 05:11:34 GMT
  • Further comments disabled!
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency