Reputation: 487
I am just new to python.
as i know in python multi-line comments are made like this
"""
this is a comments
"""
but consider this code
somevariable="""this is a variable"""
print somevariable
it prints this is a variable
are comments and strings are same in python or they are different?
Upvotes: 3
Views: 127
Reputation: 8244
The confusion comes through docstrings.
This is actually not a comment, but a way to make a string with multiple lines:
>>> """This is a string
... with line breaks"""
'This is a string\nwith line breaks'
The same notation is used in classes and functions to document them:
>>> def my_function():
... """This function does stupid things."""
... return 1/0
...
>>> my_function.__doc__
'This function does stupid things.'
>>> my_function()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in my_function
ZeroDivisionError: division by zero
So your code:
somevariable = """this is a variable"""
is really just equal to:
somevariable = "this is a variable"
Upvotes: 1
Reputation: 64318
are comments and strings the same in python?
Strictly speaking, they are not the same. But in your question, what you refer to as "a comment" is actually a string. A comment is everything appearing in the same line after the #
character.
a.py:
"""
comment1
"""
# comment2
Now:
% strings a.pyc | grep comment
comment1
As you can see, comment2
is not part of the compiled byte code, while comment1
is -- because it's not a comment, it's a string.
As others pointed out, the string is considered a docstring
. docstring
s are conventionally used for documenting your code, while comments are used, well, for commenting, and are not included in the documentation.
Upvotes: 2
Reputation: 21595
>>> somevariable="""
... this is not a comment
... """
>>> print(somevariable)
this is not a comment
Upvotes: 1
Reputation: 2428
The first one isn't a comment, it's a string. But since you're not doing anything with that string (printing it, assigning it to a variable etc.) all the interpreter does is say "Yup, that's a string alright!" and ignore it entirely. Effectively making it the same as a comment, since it's an arbitrary bunch of text that's ignored by the interpreter.
Upvotes: 5
Reputation: 599600
The first snippet is not a comment. It is a string. Comments in Python are prefixed by the #
character.
Upvotes: 1
Reputation: 81
Yes, they are the same
It is called a docstring You can find more information about this type of string here: http://www.pythonforbeginners.com/basics/python-docstrings/
Upvotes: 2
Reputation: 8104
Yes, they are the same. It is a convention to include a comment string as the first statement of the function or a class.
See PEP 257 -- Docstring Conventions.
Upvotes: 1
Reputation: 142146
There are no multi-line comments in Python. They're both just strings. The former is typically used as a docstring and will be associated with the function/class/module, while the other gets assigned to a variable.
Upvotes: 4