0% found this document useful (0 votes)
3 views7 pages

Understanding Python Comments

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)
3 views7 pages

Understanding Python Comments

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

10/5/25, 8:16 AM Python Comments

 Tutorials  References  Exercises  Sign 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/7
10/5/25, 8:16 AM Python Comments

print("Hello,
Tutorials 
World!") #This is a comment
References  Exercises  Sign 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 »

ADVERTISEMENT

REMOVE ADS

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!")

[Link] 2/7
10/5/25, 8:16 AM Python Comments

Try it Yourself »
Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Or, not quite as intended, you can use a multiline string.

Since Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it:

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:

'

//

/*

[Link] 3/7
10/5/25, 8:16 AM Python Comments

 Tutorials  References  Exercises 


Submit Answer »
Sign In

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

Video: Python Comments

❮ Previous Sign in to track progress Next ❯

ADVERTISEMENT

[Link] 4/7
10/5/25, 8:16 AM Python Comments

 Tutorials  References  Exercises  Sign In

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

COLOR PICKER

 

ADVERTISEMENT

REMOVE ADS

ADVERTISEMENT

ADVERTISEMENT

 

 PLUS SPACES

[Link] 5/7
10/5/25, 8:16 AM Python Comments

 Tutorials  References 
GET CERTIFIED
Exercises 
FOR TEACHERS
Sign In

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

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
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
AngularJS 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

    
[Link] 6/7
10/5/25, 8:16 AM Python Comments

FORUM ABOUT ACADEMY


 Tutorials  References  Exercises 
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
Sign In

learning.
HTML
 CSSTutorials,JAVASCRIPT SQL are constantly
references, and examples PYTHON reviewedJAVA PHPbut weHOW
to avoid errors, cannotTO [Link]
warrant full C
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] 7/7

You might also like