views:

10786

answers:

18

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?

A: 

Your team's taste or your project's coding guidelines.

If you are in a multilanguage environment, you might wish to encourage the use of the same type of quotes for strings that the other language uses, for instance. Else, I personally like best the look of '

Vinko Vrsalovic
A: 

None as far as I know. Although if you look at some code, " " is commonly used for strings of text (I guess ' is more common inside text than "), and ' ' appears in hashkeys and things like that.

Mario
+12  A: 

If the string you have contains one, then you should use the other. For example, "You're able to do this", or 'He said "Hi!"'. Other than that, you should simply be as consistent as you can (within a module, within a package, within a project, within an organisation).

If your code is going to be read by people who work with C/C++ (or if you switch between those languages and Python), then using '' for single-character strings, and "" for longer strings might help ease the transition. (Likewise for following other languages where they are not interchangeable).

The Python code I've seen in the wild tends to favour " over ', but only slightly. The one exception is that """these""" are much more common than '''these''', from what I have seen.

Tony Meyer
+1  A: 

I use double quotes in general, but not for any specific reason - Probably just out of habit from Java.

I guess you're also more likely to want apostrophes in an inline literal string than you are to want double quotes.

Matt Sheppard
+13  A: 

I prefer ', especially for '''docstrings''', as I find """this creates some fluff""". Also, ' can be typed without the shift key.

Daren Thomas
I tend to prefer single quotes, since I write SQL code every day, and single quotes are used for string literals in T-SQL. But I do use triple double quotes, because docstrings can sometimes use a bit of fluff in them.
eksortso
I prefer single quotes everywhere for readability (indeed, double quotes cause more fluff). But as I come from C, I almost automatically type double quotes :)
wump
@wump, yes. My C# compiler is forever complaining about my character literals...
Daren Thomas
+56  A: 

I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed.

For example:

LIGHT_MESSAGES = {
    'English': "There are %(number_of_lights)s lights.",
    'Pirate':  "Arr! There be %(number_of_lights)s lights."
}

def lights_message(language, number_of_lights):
    """Return a language-appropriate string reporting the light count."""
    return LIGHT_MESSAGES[language] % locals()

def is_pirate(message):
    """Return True if the given message sounds piratical."""
    return re.search(r"(?i)(arr|avast|yohoho)!", message) is not None
Will Harris
+1 for the example
Paolo Bergantino
Interesting, I use them in exactly the same way. I don't remember ever reading anything to nudge me in that direction. I also use triple single quotes for long string not intended for humans, like raw html. Maybe it's something to do with English quote rules.
Most python coders code it that way. There is no explicit rule, but because we often read the code that way, it becomes an habit.
e-satis
except for the regex I do the same thing
igorgue
I wonder if the single quotes for symbol-like things actually comes from the quote expression shortcut in Lisp/Scheme. In any case, it's intuitive.Also, me mateys, if we're following PEP 8 style guidelines, the functions really should be named lights_message() and is_pirate().
yukondude
+11  A: 

I'm with Will:

  • Double quotes for text
  • Single quotes for anything that behaves like an identifier
  • Double quoted raw string literals for regexps
  • Tripled double quotes for docstrings

I'll stick with that even if it means a lot of escaping.

I get the most value out of single quoted identifiers standing out because of the quotes. The rest of the practices are there just to give those single quoted identifiers some standing room.

Garth T Kidd
A: 

I agree with Daren. Single quotes are great since they do not require the shift key.

Bartosz Radaczyński
+1  A: 

It's probably a stylistic preference more than anything. I just checked PEP 8 and didn't see any mention of single versus double quotes.

I prefer single quotes because its only one keystroke instead of two. That is, I don't have to mash the shift key to make single quote.

Mike
+5  A: 

Quoting the official docs at http://docs.python.org/ref/strings.html:

In plain English: String literals can be enclosed in matching single quotes (') or double quotes (").

So there is no difference. Instead, people will tell you to choose whichever style that matches the context, and to be consistent. And I would agree - adding that it is pointless to try to come up with "conventions" for this sort of thing because you'll only end up confusing any newcomers.

conny
+1  A: 

Personally I stick with one or the other. It doesn't matter. And providing your own meaning to either quote is just to confuse other people when you collaborate.

schwa
A: 

I chose to use double quotes because they are easier to see.

Andrew Dalke
A: 

I just use whatever strikes my fancy at the time; it's convenient to be able to switch between the two at a whim!

Of course, when quoting quote characetrs, switching between the two might not be so whimsical after all...

+3  A: 

Triple quoted comments are an interesting subtopic of this question. PEP 257 specifies triple quotes for doc strings. I did a quick check using Google Code Search and found that triple double quotes in Python are about 10x as popular as triple single quotes -- 1.3M vs 131K occurrences in the code Google indexes. So in the multi line case your code is probably going to be more familiar to people if it uses triple double quotes.

jblocksom
A: 

' = "

/ = \ = \ example :

    f = open('c:\word.txt', 'r')
    f = open("c:\word.txt", "r")
    f = open("c:/word.txt", "r")
    f = open("c:\\\word.txt", "r")

Results are the same

=>> no, they're not the same. A single backslash will escape characters. You just happen to luck out in that example because \k and \w aren't valid escapes like \t or \n or \ or \"

If you want to use single backslashes (and have them interpreted as such), then you need to use a "raw" string. You can do this by putting an 'r' in front of the string

im_raw = r'c:\temp.txt'
non_raw = 'c:\\temp.txt'
another_way = 'c:/temp.txt'

As far as paths in Windows are concerned, forward slashes are interpreted the same way. Clearly the string itself is different though. I wouldn't guarantee that they're handled this way on an external device though.

python
A: 

Lots of answers about style and semantics here, but how about something concrete?

In Perl you want to use single quotes when you have a string which doesn't need to interpolate variables or escaped characters like \n, \t, \r, etc.

It makes the code compile faster, because the parser knows it doesn't have to examine single quoted strings for escapes.

I would hope Python is built the same way.

NoMan
nope, escapes work in single- and double-quoted strings in python. and anyway, neither language is compiled, optimised bytecode will reflect the same string. It all just gets passed along to the operating system, where the appropriate library handles everything.
stefano palazzo
A: 

In Perl you want to use single quotes when you have a string which doesn't need to interpolate variables or escaped characters like \n, \t, \r, etc.

PHP makes the same distinction as Perl: content in single quotes will not be interpreted (not even \n will be converted), as opposed to double quotes which can contain variables to have their value printed out.

Python does not, I'm afraid. Technically seen, there is no $ token (or the like) to separate a name/text from a variable in Python. Both features make Python more readable, less confusing, after all. Single and double quotes can be used interchangeably in Python.

Peterino
A: 

I use double quotes because I have been doing so for years in most languages (C++, Java, VB…) except Bash, because I also use double quotes in normal text and because I'm using a (modified) non-English keyboard where both characters require the shift key.

Philipp