0% found this document useful (0 votes)
7 views6 pages

Understanding Python Comments

The document explains the use of comments in Python, which can clarify code, enhance readability, and prevent execution during testing. It describes how to create single-line and multiline comments using the '#' character and multiline strings. Additionally, it provides examples to illustrate these concepts.

Uploaded by

nshansundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Understanding Python Comments

The document explains the use of comments in Python, which can clarify code, enhance readability, and prevent execution during testing. It describes how to create single-line and multiline comments using the '#' character and multiline strings. Additionally, it provides examples to illustrate these concepts.

Uploaded by

nshansundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3/28/25, 4:13 PM Python Comments

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Python Comments
❮ Previous Next ❯

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a Comment
Comments starts with a # , and Python will ignore them:

Example Get your own Python Server

#This is a comment
print("Hello, World!")

Try it Yourself »

Comments can be placed at the end of a line, and Python will ignore the rest of the line:

Example
[Link] 1/6
3/28/25, 4:13 PM Python Comments

print("Hello,
Tutorials 
World!") #This is a comment
Exercises  Services   Sign Up Log in

HTML
 Try itCSS
YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO [Link] C

A comment does not have to be text that explains the code, it can also be used to
prevent Python from executing code:

Example

#print("Hello, World!")
print("Cheers, Mate!")

Try it Yourself »

Multiline Comments
Python does not really have a syntax for multiline comments.

To add a multiline comment you could insert a # for each line:

Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")

Try it Yourself »

Or, not quite as intended, you can use a multiline string.

[Link] 2/6
3/28/25, 4:13 PM Python Comments

Since Python will ignore string literals that are not assigned to a variable, you can add a
 multilineTutorials 
string (tripleExercises Services
quotes)in your  comment inside
code, and place your Signit:
Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Example

"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

Try it Yourself »

As long as the string is not assigned to a variable, Python will read the code, but then
ignore it, and you have made a multiline comment.

?
Exercise
Which character is used to define a Python comment:

'

//

/*

Submit Answer »

[Link] 3/6
3/28/25, 4:13 PM Python Comments

 Tutorials  Video:
Exercises  Python
Services  Comments
 Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

[Link] 4/6
3/28/25, 4:13 PM Python Comments

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

COLOR PICKER



 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
[Link] 5/6
3/28/25, 4:13 PM Python Comments
HTML Tutorial

 Tutorials  CSS Tutorial


Exercises 
JavaScript Tutorial
Services   Sign Up Log in
How To Tutorial
HTML
 CSS SQL Tutorial SQL
JAVASCRIPT PYTHON JAVA PHP HOW TO [Link] C
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 6/6

You might also like