Reputation: 52911
What is the difference between apostrophes and quotation marks in Python?
So far I've only been able to find one difference
print "'"
print '"'
print '''
print """
The first print statement will output ' while the second ". However the third statement starts a comment block.
Any other differences I should be aware of?
Upvotes: 6
Views: 11090
Reputation: 90995
'...' strings and "..." are identical except that you don't need to escape '
inside a "
string, nor vice versa.
Triple quotes start a multi-line string. These are often used for docstrings, which is probably where you got the idea that they're comments.
Upvotes: 4
Reputation: 131627
print 'Hello'
and print "Hello"
are the same and what you use is your personal preference. """
and '''
are for multiline strings.
>>> print """First
Second
Third"""
First
Second
Third
Upvotes: 16
Reputation: 7102
Note: in PHP string quoted by single-quotes ' don't interpret escapes like '\n' '\r' etc. In python you can use 'r' modifier. So, str=r'some \n string containing "\n" inside'
- is RAW string and it does not contain newline characters.
Upvotes: 1
Reputation: 66709
Python has a facility multiline string that starts with triple quotes.
They are also commonly used for docstrings.
An example of multiline string:
>>> x = """ wdd2ed
... 2wdqd
... d
... dd
... d
... """
>>>
>>> print x
wdd2ed
2wdqd
d
dd
d
>>>
String literals can be enclosed in matching single quotes (') or double quotes ("). So "string" and 'string' are same.
The following provides all the details: http://docs.python.org/reference/lexical_analysis.html#string-literals
Upvotes: 8
Reputation: 31586
'
and "
can be used interchangably to start and end a string (be sure to close with the same one you opened with). It's provided as a convenience so you don't need to escape quote in most cases.
For example, if you wanted the string say "hello"
in languages that don't provide the alternative option, you would need to escape it with something like "say \"hello\""
which is somewhat ugly compared to 'say "hello"'
Likewise if you could only use '
(not sure of any language that does this) then a string to say bill's pony
would be 'bill\'s pony'
instead of "bill's pony"
.
Upvotes: 3
Reputation: 375554
Triple-quotes aren't for comments, they are the syntax for multi-line strings. They are often used for docstrings, which serve a similar role as block comments in other languages. But multi-line strings can be used as data just as the other string syntaxes can.
Upvotes: 5