{"id":10315,"date":"2021-08-31T18:13:28","date_gmt":"2021-08-31T12:43:28","guid":{"rendered":"https:\/\/pynative.com\/?p=10315"},"modified":"2022-08-22T12:20:24","modified_gmt":"2022-08-22T06:50:24","slug":"python-comments","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-comments\/","title":{"rendered":"Python Comments"},"content":{"rendered":"\n<p><strong>Comments describe what is happening inside a program<\/strong> so that a person looking at the source code does not have difficulty figuring it out.<\/p>\n\n\n\n<p>In this article, you&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>How to add comments in your Python code<\/li><li>The need for the comments<\/li><li>What are the inline comments, block comments, and multi-line comments<\/li><li>The use of docstring comments.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-what-is-comment-in-python\" data-level=\"2\">What is Comment in Python?<\/a><\/li><li><a href=\"#h-single-line-comment\" data-level=\"2\">Single-line Comment<\/a><\/li><li><a href=\"#h-multi-line-comments\" data-level=\"2\">Multi-Line Comments <\/a><\/li><li><a href=\"#h-add-sensible-comments\" data-level=\"2\">Add Sensible Comments<\/a><\/li><li><a href=\"#h-inline-comments\" data-level=\"2\">Inline Comments<\/a><\/li><li><a href=\"#h-block-comments\" data-level=\"2\">Block Comments<\/a><\/li><li><a href=\"#h-docstring-comments\" data-level=\"2\">Docstring Comments<\/a><\/li><li><a href=\"#h-commenting-out-code-for-testing\" data-level=\"2\">Commenting Out Code for Testing<\/a><\/li><li><a href=\"#h-using-string-literals-for-multi-line-comments\" data-level=\"2\">Using String Literals for Multi-line Comments<\/a><\/li><li><a href=\"#h-summary\" data-level=\"2\">Summary<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-comment-in-python\">What is Comment in Python?<\/h2>\n\n\n\n<p>The comments are descriptions that help programmers to understand the functionality of the program. Thus, comments are necessary while writing code in Python.<\/p>\n\n\n\n<p>In Python, we <strong>use the hash (<code>#<\/code>) symbol to start writing a comment<\/strong>. The comment begins with a hash sign (<code>#<\/code>) and whitespace character and continues to the end of the line.<\/p>\n\n\n\n<p>Many programmers commonly use Python for task automation, data analysis, and data visualization. Also, Python has been adopted by many non-programmers such as analysts and scientists.<\/p>\n\n\n\n<p>In a team, many programmers work together on a single application. Therefore, if you are developing new code or modifying the existing application&#8217;s code, it is essential to describe the purpose behind your code using comments.<\/p>\n\n\n\n<p>For example, if you have added new functions or classes in the source code, describe them. It helps other colleagues understand the purpose of your code and also helps in <strong>code review<\/strong>.<\/p>\n\n\n\n<p>Apart from this, In the future, it helps to <strong>find and fix the errors<\/strong>, <strong>improve the code later on<\/strong>, and reuse it in many different applications.<\/p>\n\n\n\n<p>Writing comments is considered a good practice and required for <strong>code quality<\/strong>. <\/p>\n\n\n\n<p>If a comment is found while executing a script, the Python interpreter completely ignores it and moves to the next line.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">x = <span class=\"hljs-number\">10<\/span>\ny = <span class=\"hljs-number\">20<\/span>\n\n<span class=\"hljs-comment\"># adding two numbers<\/span>\nz = x + y\nprint(<span class=\"hljs-string\">'Sum:'<\/span>, z)\n\n<span class=\"hljs-comment\"># Output 30<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As you can see in the above program, we have added the comment &#8216;adding two numbers&#8217;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-single-line-comment\">Single-line Comment<\/h2>\n\n\n\n<p>Python has two types of comments single-line and multi-line comments.<\/p>\n\n\n\n<p>In Python, single-line comments are indicated by a hash sign(<code>#<\/code>). The interpreter ignores anything written after the # sign, and it is effective till the end of the line.<\/p>\n\n\n\n<p>Primarily these comments are written over Python statements to clarify what they are doing.<\/p>\n\n\n\n<p><strong>Example: <\/strong>Writing single-line Comments<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># welcome message<\/span>\nprint(<span class=\"hljs-string\">'Welcome to PYnative...'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Welcome to PYnative...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>: By considering the readability of a code, limit the comment line to a maximum of 79 characters as per the <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\/#maximum-line-length\" target=\"_blank\" rel=\"noreferrer noopener\">PEP 8 style guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-multi-line-comments\">Multi-Line Comments <\/h2>\n\n\n\n<p>In Python, there is no separate way to write a multi-line comment. Instead, we need to use a hash sign at the beginning of each comment line to make it a multi-line comment<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># This is a<\/span>\n<span class=\"hljs-comment\"># multiline<\/span>\n<span class=\"hljs-comment\"># comment<\/span>\nprint(<span class=\"hljs-string\">'Welcome to PYnative...'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Welcome to PYnative...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-add-sensible-comments\">Add Sensible Comments<\/h2>\n\n\n\n<p>A comment must be short and straightforward, and sensible. A comment must add value to your code. <\/p>\n\n\n\n<p>You should add comments to give code overviews and provide additional information that is not readily available in the code itself.<\/p>\n\n\n\n<p>Comments should contain only information that is relevant to reading and understanding the program.<\/p>\n\n\n\n<p>Let&#8217;s take the following example.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Define list of student names<\/span>\nnames = &#91;<span class=\"hljs-string\">'Jess'<\/span>, <span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-string\">'Kelly'<\/span>]\n\n<span class=\"hljs-comment\"># iterates over name list and prints each name<\/span>\n<span class=\"hljs-keyword\">for<\/span> student <span class=\"hljs-keyword\">in<\/span> names:\n   print(student, end=<span class=\"hljs-string\">' '<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see, the code is self-explanatory, and adding comments for such code is unnecessary. It would be best if you avoided such scenarios.<\/p>\n\n\n\n<p>Now, let&#8217;s take the second example where we have demonstrated the correct way to write comments.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Returns welcome message for a customer by customer name and location<\/span>\n<span class=\"hljs-comment\"># param name - Name of the customer<\/span>\n<span class=\"hljs-comment\"># param region - location<\/span>\n<span class=\"hljs-comment\"># return - Welcome message<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name, region)<\/span>:<\/span>\n    message = get_message(region)\n    <span class=\"hljs-keyword\">return<\/span> message + <span class=\"hljs-string\">\" \"<\/span> + name\n\n\n<span class=\"hljs-comment\"># Returns welcome message by location<\/span>\n<span class=\"hljs-comment\"># param region - location<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_message<\/span><span class=\"hljs-params\">(region)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (region == <span class=\"hljs-string\">'USA'<\/span>):\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hello'<\/span>\n    <span class=\"hljs-keyword\">elif<\/span> (region == <span class=\"hljs-string\">'India'<\/span>):\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Namaste'<\/span>\n\nprint(greet(<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'USA'<\/span>))\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-inline-comments\">Inline Comments<\/h2>\n\n\n\n<p>We can add concise comments on the same line as the code they describe but should be shifted enough to separate them from the statements for better readability. It is also called a trailing comment.<\/p>\n\n\n\n<p>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 <code>#<\/code> and a single space.<\/p>\n\n\n\n<p>Inline comments are useful when you are using any formula or any want to explain the code line in short.<\/p>\n\n\n\n<p>If one or more lines contain the short comment, they should all be indented to the same tab setting.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">calculate_bonus<\/span><span class=\"hljs-params\">(salary)<\/span>:<\/span>\n    salary = salary * <span class=\"hljs-number\">7.5<\/span>  <span class=\"hljs-comment\"># yearly bonus percentage 7.5<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-block-comments\">Block Comments<\/h2>\n\n\n\n<p>Block comments are used to provide descriptions of files, <a href=\"https:\/\/pynative.com\/python-classes-and-objects\/\">classes<\/a>, and <a href=\"https:\/\/pynative.com\/python-functions\/\">functions<\/a>. We should add block comments at the beginning of each file and before each method.<\/p>\n\n\n\n<p>Add a black line after the block comment to set it apart from the rest of the code.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Returns welcome message for a customer by customer name and location<\/span>\n<span class=\"hljs-comment\"># param name - Name of the customer<\/span>\n<span class=\"hljs-comment\"># param region - location<\/span>\n<span class=\"hljs-comment\"># return - Welcome message<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name, region)<\/span>:<\/span>\n    message = get_message(region)\n    <span class=\"hljs-keyword\">return<\/span> message + <span class=\"hljs-string\">\" \"<\/span> + name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-docstring-comments\">Docstring Comments<\/h2>\n\n\n\n<p>Docstring comments describe Python <a href=\"https:\/\/pynative.com\/python-classes-and-objects\/\">classes<\/a>, <a href=\"https:\/\/pynative.com\/python-functions\/\">functions<\/a>, <a href=\"https:\/\/pynative.com\/python-constructors\/\">constructors<\/a>, <a href=\"https:\/\/pynative.com\/python-instance-methods\/\">methods<\/a>. The docstring comment should appear just after the declaration. Although not mandatory, this is highly recommended.<\/p>\n\n\n\n<p>Conventions for writing good documentation strings are mentioned in&nbsp;<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0257\" target=\"_blank\" rel=\"noreferrer noopener\">PEP 257<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The docstring comment should appear just after the declaration.<\/li><li>A docstring can be a single line or a multi-line comment.<\/li><li>Docstring should begin with a capital letter and end with a period.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>: docstring in function<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">bonus<\/span><span class=\"hljs-params\">(salary)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\"Calculate the bonus 10% of a salary .\"\"\"<\/span>\n    <span class=\"hljs-keyword\">return<\/span> salary * <span class=\"hljs-number\">10<\/span> \/ <span class=\"hljs-number\">100<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-commenting-out-code-for-testing\">Commenting Out Code for Testing<\/h2>\n\n\n\n<p>If you are getting a runtime error or not getting an expected output and cannot figure out which part of the code is causing the problem, you can comment out the specific block or a line to quickly figure out the problem.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name, region)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># below code is comment for testing<\/span>\n    <span class=\"hljs-comment\"># message = get_message(region)<\/span>\n    message= <span class=\"hljs-string\">'Hello'<\/span>\n    <span class=\"hljs-keyword\">return<\/span> message + <span class=\"hljs-string\">\" \"<\/span> + name\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_message<\/span><span class=\"hljs-params\">(region)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (region == <span class=\"hljs-string\">'USA'<\/span>):\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hello'<\/span>\n    <span class=\"hljs-keyword\">elif<\/span> (region == <span class=\"hljs-string\">'India'<\/span>):\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Namaste'<\/span>\n\nprint(greet(<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'USA'<\/span>))\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-string-literals-for-multi-line-comments\">Using String Literals for Multi-line Comments<\/h2>\n\n\n\n<p>As we discussed, there is no unique way to write multi-line comments. We can use multi-line strings (triple quotes) to write multi-line comments. The quotation character can either be &#8216; or &#8220;.<\/p>\n\n\n\n<p>Python interpreter ignores the string literals that are not assigned to a variable.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-string\">'''\n\nI am a\nmultiline comment!\n\n'''<\/span>\nprint(<span class=\"hljs-string\">\"Welcome to PYnative..\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above example, the multi-line string isn&#8217;t assigned to any variable, so the interpreter ignores it. Even it is not technically a multi-line comment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\">Summary<\/h2>\n\n\n\n<p>The comments are descriptions that help programmers to understand the functionality of the program. Thus, comments are necessary while writing code in Python.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the hash (#) symbol to start writing a comment in Python<\/li><li>Comments should contain only information that is relevant to reading and understanding the program.<\/li><li>Python dosen&#8217;t support multi-line comments. we need to use a hash sign at the beginning of each comment line to make it a multi-line comment<\/li><li>Keep comments indentation uniform and match for best readability.<\/li><li>Add Docstring to functions and classes<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Comments describe what is happening inside a program so that a person looking at the source code does not have difficulty figuring it out. In this article, you&#8217;ll learn: How to add comments in your Python code The need for the comments What are the inline comments, block comments, and multi-line comments The use of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,1],"tags":[32],"class_list":{"0":"post-10315","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-basics","8":"tag-python-basics","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Comments [Guide] &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-comments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Comments\" \/>\n<meta property=\"og:description\" content=\"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-comments\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-31T12:43:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-22T06:50:24+00:00\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python Comments\",\"datePublished\":\"2021-08-31T12:43:28+00:00\",\"dateModified\":\"2022-08-22T06:50:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/\"},\"wordCount\":1020,\"commentCount\":11,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"keywords\":[\"Python Basics\"],\"articleSection\":[\"Python\",\"Python Basics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-comments\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/\",\"name\":\"Python Comments [Guide] &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"datePublished\":\"2021-08-31T12:43:28+00:00\",\"dateModified\":\"2022-08-22T06:50:24+00:00\",\"description\":\"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-comments\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-comments\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Comments\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Comments [Guide] &#8211; PYnative","description":"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pynative.com\/python-comments\/","og_locale":"en_US","og_type":"article","og_title":"Python Comments","og_description":"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.","og_url":"https:\/\/pynative.com\/python-comments\/","og_site_name":"PYnative","article_published_time":"2021-08-31T12:43:28+00:00","article_modified_time":"2022-08-22T06:50:24+00:00","author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-comments\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-comments\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python Comments","datePublished":"2021-08-31T12:43:28+00:00","dateModified":"2022-08-22T06:50:24+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-comments\/"},"wordCount":1020,"commentCount":11,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"keywords":["Python Basics"],"articleSection":["Python","Python Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-comments\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-comments\/","url":"https:\/\/pynative.com\/python-comments\/","name":"Python Comments [Guide] &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"datePublished":"2021-08-31T12:43:28+00:00","dateModified":"2022-08-22T06:50:24+00:00","description":"Learn Commenting Python Code. Understand the need for comments. Write single-line and multi-line comments. Understand block, inline, and docstring comments.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-comments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-comments\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-comments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Python Comments"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/10315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=10315"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/10315\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=10315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=10315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=10315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}