Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python Single and Multi-Line Comments with Shortcuts

In Python (or any other programming language), comments are used to explain the source code. Comments describe the code, which helps in maintaining the application in the future for ourselves and others. In Python, comments come in two primary forms: single-line comments and multiple-line comments. Let us learn …

Lokesh Gupta

September 21, 2023

Python Basics
Python Basics
Python comments

In Python (or any other programming language), comments are used to explain the source code. Comments describe the code, which helps in maintaining the application in the future for ourselves and others.

In Python, comments come in two primary forms: single-line comments and multiple-line comments. Let us learn in detail.

# single line comment using hash character
print(2 + 2)

print(2 + 3)	# another single-line comment

"multi-line comments
are
written within double quotes"

print(2 + 2)

1. Single-Line Comments in Python

Single-line comments in Python begin with a # (hash) symbol and continue until the end of the line. When executing the program, Python ignores everything after the # symbol on a line. Single-line comments are ideal for adding brief explanations or notes to specific lines of code.

Here’s an example of a single-line comment in Python:

# This is a single-line comment
x = 10  # Assigning the value 10 to the variable x

2. Multi-line Comments in Python

Python does not provide a built-in syntax for traditional block comments (such as Java comments). However, developers often use multi-line strings enclosed in triple quotes (single or double) as a workaround to create multiple-line comments.

While these are not officially the comments, these docstrings serve the purpose of providing explanations for larger blocks of code. Python ignores string literals that are not assigned to any variable, and so it will not affect the program execution.

'''
This is a multi-line comment.
It provides detailed explanations or documentation
for a block of code or function.
'''
def some_function():
    # Code for the function
    pass

Otherwise, we can use the # character to write multi-line comments as well.

# This is a multi-line comment.
# It provides detailed explanations or documentation
# for a block of code or function.

def some_function():
    # Code for the function
    pass

3. Commenting Shortcuts

Python also offers some convenient shortcuts for commenting code. These shortcuts are helpful when we want to temporarily disable or comment out a block of code during debugging or testing:

We can comment out multiple lines of code by selecting them and then pressing Ctrl + / (on Windows/Linux) or Cmd + / (on macOS) in many popular code editors and IDEs.

This action inserts # symbols at the beginning of each selected line, effectively commenting them out. Repeating the same shortcut uncommented the lines.

Ctrl + /  # Windows and Linux

Cmd + / # MacOS

Drop me your questions related to writing comments in Python.

Happy Learning !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python Examples

Python Flow Control

  • Python if…else
  • Python for Loop
  • Python while Loop
  • Python break
  • Python continue
  • Python pass

Python Datatypes

  • Python Integer
  • Python String
  • Python List
  • Python Tuple
  • Python Set
  • Python Dictionary
  • Python OrderedDict
  • Python Priority Queue

Python Modules

  • Python Bcrypt
  • Python Hashlib
  • Python Httplib2
  • Python JSON

Python Advanced Topics

  • Python CSV Files
  • Building a Recommendation System

Python Reference

  • Built-in Functions
  • String Functions
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java IntPredicate Example

Next

Python Variables (with Examples)

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap