It appears your teacher is a fan of How to Design Programs ;)

I'd tackle this as writing for two different audiences who won't always overlap.

First there are the docstrings; these are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use)

Secondly there are in-code comments; these are to explain what is going on to people (generally you!) who want to extend the code. These will not normally be turned into documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind)
  • Workarounds for code you don't have control over, that may otherwise appear illogical
  • I'll admit to TODOs as well, though I try to keep that to a minimum
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical

Since you're coding in an academic setting, and it sounds like your lecturer is going for verbose, I'd say just roll with it. Use code comments to explain how you are doing what you say you are doing in the design recipe.

Answer from dejester on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › docstring vs comments
r/learnpython on Reddit: Docstring vs Comments
September 11, 2024 - Docstrings are easily obtainable by other Python tools dynamically just by inspecting your objects. This is useful for tools that do things like generating API documentation. Comments are, by comparison, more difficult for such tools to use in part because comments are discarded by the compiler whereas docstrings are a part of your object (see .__doc__ attribute of any function, class, etc.).
Top answer
1 of 5
56

It appears your teacher is a fan of How to Design Programs ;)

I'd tackle this as writing for two different audiences who won't always overlap.

First there are the docstrings; these are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use)

Secondly there are in-code comments; these are to explain what is going on to people (generally you!) who want to extend the code. These will not normally be turned into documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind)
  • Workarounds for code you don't have control over, that may otherwise appear illogical
  • I'll admit to TODOs as well, though I try to keep that to a minimum
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical

Since you're coding in an academic setting, and it sounds like your lecturer is going for verbose, I'd say just roll with it. Use code comments to explain how you are doing what you say you are doing in the design recipe.

2 of 5
10

I believe that it's worth to mention what PEP8 says, I mean, the pure concept.

Docstrings

Conventions for writing good documentation strings (a.k.a. "docstrings") are immortalized in PEP 257.

Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does. This comment should appear after the def line.

PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, e.g.:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.
"""

For one liner docstrings, please keep the closing """ on the same line.

Comments

Block comments

Generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

Paragraphs inside a block comment are separated by a line containing a single #.

Inline Comments

Use inline comments sparingly.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Inline comments are unnecessary and in fact distracting if they state the obvious.

Don't do this:

x = x + 1 # Increment x

But sometimes, this is useful:

x = x + 1 # Compensate for border

Reference

  • https://www.python.org/dev/peps/pep-0008/#documentation-strings
  • https://www.python.org/dev/peps/pep-0008/#inline-comments
  • https://www.python.org/dev/peps/pep-0008/#block-comments
  • https://www.python.org/dev/peps/pep-0257/
Discussions

api documentation - When documenting Python, when should I use docstrings and when should I use comments? - Writing Stack Exchange
Python programming language provides two mechanisms for documenting a function, a module or a class: Comments and Documentation strings (or Docstring). Both can be accessed by reading the source co... More on writing.stackexchange.com
🌐 writing.stackexchange.com
January 24, 2018
Docstring vs. Comment
I hope y’all don’t get annoyed with me. I’m wondering what’s the difference between a doctoring and a comment at the beginning of a program. For example: ''' This program prints a user's age ''' vs # This program prints a user's age More on discuss.python.org
🌐 discuss.python.org
4
0
October 20, 2021
What's the difference between comments and docstring in python?
for example while running a code . the problem is I find both similar ..how can I use them in programs? More on sololearn.com
🌐 sololearn.com
3
1
python comment in docstring - Stack Overflow
I found this one out because some of the homework questions I met were tested by docstrings, and it gives me failures. For example: def foo(x): """ >>> foo(5) 25 >>... More on stackoverflow.com
🌐 stackoverflow.com
🌐
PythonForBeginners
pythonforbeginners.com › home › difference between comments and docstrings in python
Difference between comments and docstrings in Python - PythonForBeginners.com
April 14, 2021 - We should keep in mind that comments written using # sign need not follow indentation rules but comments written using multiline strings must follow the indentation of the block in which they are declared. A docstring is a string constant associated with any python object or module.
🌐
ZetCode
zetcode.com › python › comments-docstrings
Python Comments and Docstrings - Complete Guide
Use docstrings to document interface (what), comments to explain implementation (how). Follow PEP 8 and PEP 257 style guidelines. Choose a docstring format and stick with it consistently throughout your project. Learn more from these resources: PEP 257 Docstring Conventions, Google Python Style Guide, and Sphinx Documentation.
Top answer
1 of 2
21

PEP 8 -- Style Guide for Python Code categories comments and document strings (a.k.a. docstrings) under comments sections.

Comments

  1. Block Comments
  2. Inline Comments
  3. Documentation Strings

Block comments generally apply to some (or all) code that follows them and are indented to the same level as that code.

Inline comments are unnecessary and in fact distracting if they state the obvious.

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.


Now to answer your question

Docstrings are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use).

In-code comments are to explain what is going on to people those who want to extend the code. These will not normally be turned into the documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal (credits) rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind).
  • Workarounds for the code you don't have control over, that may otherwise appear illogical.
  • I'll admit to TODOs as well, though I try to keep that to a minimum.
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical.
2 of 2
9

Code comments and docstrings have different purposes and audiences:

  • Developers write docstrings to describe the function's behaviour. Other developers, who use this function, read docstrings to find about the meaning of parameters, the pre- and post-conditions, possible exceptions etc.

    If you're writing an API, you may want to publish docstrings as a part of documentation, but have your code and code comments private.

  • Developers write comments to describe the code's inner logic, when this logic isn't clear from just reading the code. The audience is themselves and other developers, who will modify this function in the future.

🌐
PythonForBeginners
pythonforbeginners.com › home › when to use comments vs. docstrings in python
When to Use Comments vs. Docstrings in Python - PythonForBeginners.com
July 27, 2021 - String comments can be many lines long. Python will ignore them when you run the program. """ With string comments, there’s no limit on how long your comment can be. But there is a need for caution. String comments can be mistaken for docstrings if you put them in the wrong place.
🌐
Python.org
discuss.python.org › python help
Docstring vs. Comment - Python Help - Discussions on Python.org
October 20, 2021 - I hope y’all don’t get annoyed with me. I’m wondering what’s the difference between a doctoring and a comment at the beginning of a program. For example: ''' This program prints a user's age ''' vs # This program prin…
Find elsewhere
🌐
Prodigiouspython
prodigiouspython.github.io › ProdigiousPython › prodigiouspython › Chapter_3 › 1_Comments_and_docstrings.html
14. Comments and Docstrings — Prodigious Python 🐍
... Sometimes we need to write a huge explanation using comments, in those cases we do use multi-line comments. multiline comments are enclosed in """ """ or ''' ''' ... Docstrings are specific type of comments that are stored as a attribute to the module, class, method or function.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › comments, docstrings, and type hints in python code
Comments, Docstrings, and Type Hints in Python Code - MachineLearningMastery.com
June 21, 2022 - The string literal in Python as a comment has a special purpose if it is in the first line under a function. The string literal, in that case, is called the “docstring” of the function.
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
Python docstrings are the string literals that appear right after the definition of a function, method, class, or module. Let's take an example. def square(n): '''Take a number n and return the square of n.''' return n**2 ... Inside the triple quotation marks is the docstring of the function square() as it appears right after its definition. Note: We can also use triple """ quotations to create docstrings. ... Comments are descriptions that help programmers better understand the intent and functionality of the program.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Declared using triple quotes (' ' ' or " " "). Written just below the definition of a function, class, or module. Unlike comments (#), docstrings can be accessed at runtime using __doc__ or help().
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - Docstrings are strings used to document your Python code and can be accessed at runtime. Python comments and docstrings have important differences.
🌐
Python
peps.python.org › pep-0257
PEP 257 – Docstring Conventions | peps.python.org
This PEP documents the semantics and conventions associated with Python docstrings.
🌐
Jaraco
blog.jaraco.com › why-docstrings-are-preferable-to-comments
In Python, use docstrings or comments? - Jason R. Coombs
January 1, 2022 - In particular, docstrings are recommended to use triple quotes, even when the docstring is a single line, in order to facilitate easy editing to include multiple lines. In contrast, comments in Python follow the shell-style comments that only apply to a single line.
🌐
Cpske
cpske.github.io › ISP › code-quality › docstrings
Documentation in Comments | Individual Software Process
The Python convention for writing documentation in comments is called docstring.
🌐
LinkedIn
linkedin.com › all › customer-premises equipment (cpe)
How do you use docstrings and comments effectively in Python?
March 8, 2023 - Docstrings and comments are important for writing, reading, and learning from code. In Python, you can access docstrings and comments with the help() function or the __doc__ attribute to print the docstring of a function, class, or module.
🌐
Medium
medium.com › @shubhanshusharma2193 › doctring-v-s-comments-in-py-f36fe0d25986
Doctring V/S Comments in Py. Both docstrings and comments are two… | by pyguy | Medium
April 15, 2024 - In summary, docstrings are used for documentation purposes and follow specific conventions, while comments are used for annotating code and making it more readable for other developers.
🌐
Calmops
calmops.com › home › "python" › "python comments, docstrings, and documentation: best practices"
Python Comments, Docstrings, and Documentation: Best Practices - Calmops | Tech, Business & Indie Hacker Knowledge Base
May 8, 2026 - # Bad: Commented-out code # def old_function(): # return "old" # Use version control instead of leaving dead code # Bad: Excessive comments # Loop through each item for item in items: # Check if item is valid if is_valid(item): # Process the item process(item) Docstrings are string literals that document Python objects: modules, classes, functions, and methods.