What are docstrings and how do I use them to improve my code readability?

Docstrings are strings placed at the beginning of classes, functions, or .py files. They are implemented using three single quotes at the beginning and end. For example:

def Addition(a,b):

''' This function adds a and b together. '''

    return a+b

This docstring is then accessed in the interpreter using "Addition.doc"(no quotes). Docstrings are used to document how to use the class, function or script that it refers to, and so can improve code readability and maintainability.

CW
Answered by Callum W. Python tutor

1690 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How do For Loops work?


Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


When do we use a for-loop and when a while-loop?


Write a Python script to take a product name as input and then automatically google search reviews for it and open the top 3 search results in different tabs


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences