views:

1515

answers:

7

In PHP, a string enclosed in "double quotes" will be parsed for variables to replace whereas a string enclosed in 'single quotes' will not. In Python, does this also apply?

+24  A: 

No.

Milen A. Radev
A: 

There are 3 ways you can qoute strings in python: "string" 'string' """ string string """ they all produce the same result.

Vasil
in fact there is a fourth way: '''string'''
nosklo
and triple quoted strings are primarily meant for multiline comments.
camflan
+5  A: 

Python is one of the few (?) languages where ' and " have identical functionality. The choice for me usually depends on what is inside. If I'm going to quote a string that has single quotes within it I'll use double quotes and visa versa, to cut down on having to escape characters in the string.

For example, "this doesn't require escaping the '" and 'she said "quoting is easy in python"'.

Bryan Oakley
+1  A: 

There is no difference in Python, and you can really use it to your advantage when generating XML. Correct XML syntax requires double-quotes around attribute values, and in many languages, such as Java, this forces you to escape them when creating a string like this:

String HtmlInJava = "<body bgcolor=\"Pink\">"

But in Python, you simply use the other quote and make sure to use the matching end quote like this:

html_in_python = '<body bgcolor="Pink">'

Pretty nice huh? You can also use three double quotes to start and end multi-line strings, with the EOL's included like this:

multiline_python_string = """
This is a multi-line Python string which contains line breaks in the 
resulting string variable, so this string has a '\n' after the word
'resulting' and the first word 'word'."""
Steve Moyer
+4  A: 

This is almost a duplicate of:

http://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python

Vinko Vrsalovic
A: 

Single and double quoted strings in Python are identical. The only difference is that single-quoted strings can contain unescaped double quote characters, and vice versa. For example:

'a "quoted" word'
"another 'quoted' word"

Then again, there are triple-quoted strings, which allow both quote chars and newlines to be unescaped.

You can substitute variables in a string using named specifiers and the locals() builtin:

name = 'John'
lastname = 'Smith'
print 'My name is %(name)s %(lastname)s' % locals()  # prints 'My name is John Smith'
efotinis
+3  A: 

In some other languages, meta characters are not interpreted if you use single quotes. Take this example in Ruby:

irb(main):001:0> puts "string1\nstring2"
string1
string2
=> nil
irb(main):002:0> puts 'string1\nstring2'
string1\nstring2
=> nil

In Python, if you want the string to be taken literally, you can use raw strings (a string preceded by the 'r' character):

>>> print 'string1\nstring2'
string1
string2
>>> print r'string1\nstring2'
string1\nstring2
Bruno Gomes
@Bruno Gomes: in *some* other languages; certainly not all.
Bryan Oakley
@Bryan Oakley: Absolutely, thank you for the warning. It's corrected now.
Bruno Gomes