Tutorials Exercises Services Sign Up Log in
S JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ 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
print("Hello, World!") #This is a comment
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
S JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ 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.
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
""" Tutorials
This is a comment
Exercises Services Sign Up Log in
written in
S JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C#
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.
Test Yourself With Exercises
Exercise:
Comments in Python are written with a special character, which one?
This is a comment
Submit Answer »
Start the Exercise
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
Tutorials Exercises Services Sign Up Log in
S JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C#
COLOR PICKER
SPACES UPGRADE AD-FREE
NEWSLETTER GET CERTIFIED CONTACT US
Top Tutorials Top References
HTML Tutorial HTML Reference
CSS Tutorial
Tutorials Exercises
JavaScript Tutorial
Services CSS Reference
JavaScript Reference
Sign Up Log in
How To Tutorial SQL Reference
S JAVASCRIPT SQL Tutorial
SQL PYTHON JAVA PHP PythonHOW
Reference
TO W3.CSS C C++ C#
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial 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
W3.CSS 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 CLASSROOM
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-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.