Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between '' and "" in Python

Tags:

python

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?

like image 353
rectangletangle Avatar asked Nov 05 '10 18:11

rectangletangle


People also ask

What is the difference between '/' and in Python?

Difference between the '// ' and '/' in Python. Normal Division : Divides the value on the left by the one on the right. Notice that division results in a floating-point value. Floor Division : Divides and returns the integer value of the quotient.

Does Python use && or and?

To use the and operator in Python, use the keyword and instead of && because there is no && operator in Python. If you use && operator in Python, you will get the SyntaxError.

What is the difference between != and == in Python?

The = is a simple assignment operator. It assigns values from right side operands to the left side operand. While on the other hand == checks if the values of two operands are equal or not. If yes, the condition becomes true and it returns a non zero value.

What does &= mean in Python?

It means bitwise AND operation. Example : x = 5 x &= 3 #which is similar to x = x & 3 print(x)


6 Answers

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
like image 104
user225312 Avatar answered Oct 03 '22 13:10

user225312


Python has a facility multiline string that starts with triple quotes.

They are also commonly used for docstrings.

  • http://www.python.org/dev/peps/pep-0257/

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

like image 34
pyfunc Avatar answered Oct 03 '22 13:10

pyfunc


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.

like image 37
Ned Batchelder Avatar answered Oct 03 '22 12:10

Ned Batchelder


'...' 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.

like image 25
dan04 Avatar answered Oct 03 '22 14:10

dan04


' 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".

like image 38
Davy8 Avatar answered Oct 03 '22 12:10

Davy8


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.

like image 45
seriyPS Avatar answered Oct 03 '22 12:10

seriyPS