Difference between Comments and Docstrings
How does comments and docstrings work in Python? Let's check it out.
Comments are annotations used to help others understand your code. They don't affect how a program is run.
In Python, a comment can be created by inserting an "#", also known as a number sign or hash symbol. The text after it on a line will be ignored.
Unlike other programming languages, Python doesn't have a syntax for multiline comments, so you will need consecutive single-line comments if you want to express more than one idea.
Here's an example:
x = 7 y = 5 def simple_sum(x, y): #custom function called simple_sum print(x + y) #printing simple_sum simple_sum(x, y) #final result
Docstrings (documentation strings) are similar to comments, except that they have a different syntax and are retained throughout the runtime of a program. This is useful for the programmer to inspect key comments during run time.
Also, docstrings can contain multiple lines of text.
You can create docstrings by adding a multiline string containing an explanation below a function's first line, as follows:
s = input() def hashtagGen(text): """ This program will replace spaces between a random text, adding a '#' symbol to generate a hashtag. """ ht = s.replace(" ", "") return "#" + ht print(hashtagGen(s))