DEV Community

Sᴛᴀʀ⭐
Sᴛᴀʀ⭐

Posted on AI-assisted

Python Comments

Table of Contents 📑


🌟 Introduction

If you have written a lot of code or worked in a team in order to accomplish something, then you can observe after several days that even the code which was written by you becomes incomprehensible to read. This is the time when comments help you make your code readable, understandable and maintainable.


🗽 Types of Python Comments

Single-Line Comments👑✨

Comments in single lines in Python are used to make a comment or give an explanation and start with a symbol called hash (#). The interpreter will ignore everything that is written after #.

# This is a single-line comment🏍️
print("Hello, World!")  
# This prints a greeting message
Enter fullscreen mode Exit fullscreen mode

2. Multi-Line Comments🏎️

Interestingly, Python doesn't have a built-in, dedicated syntax specifically for multi-line comments. However, you can easily achieve this using two standard approaches:

Approach 1: Using 🦚Multiple Single-Line Comments

You can prefix each line with a hash (#) character:

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

Approach 2: Using Multi-Line Strings (Triple Quotes)🐻

​Alternatively, you can use a multi-line string. Since Python ignores string literals that are not assigned to a variable, you can use triple quotes (""") to add detailed notes spanning multiple lines:

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

Top comments (5)

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Love seeing more people give comments the attention they deserve. I recently wrote an article pushing back on the idea that comments are unnecessary, so this one really resonated with me.

Great write-up, keep the momentum going! You're good at it! ⭐

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ • • Edited

Learning slow! :(

Collapse
 
effessdev profile image
EffessDev •

But early start!

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ •

Yeah bro!🗽

Thread Thread
 
effessdev profile image
EffessDev •

Wait wait wait wait... You were referencing my post lol 🤣🤣🤣