Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just started a job where I'm writing Python after coming from a Java background, and I'm noticing that other developers tend to quote strings using single quotes ('') instead of double quotes (""). For example:

line1 = 'This is how strings typically look.'
line2 = "Not like this."

Is there a particular reason for this other than personal preference? Is this the proper way to be quoting strings? Specifically, what I want to know is if there is some type of standard or accepted best practice that drives this style of coding.

share|improve this question

closed as not constructive by Jarrod Roberson, ChrisF Jul 2 '12 at 10:45

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
If you ever get into Ruby, there actually is a technical difference between single and double quotes. Double quotes support string interpolation - single quotes parse as literals. –  KChaloux Oct 29 '12 at 13:09
    
The same is true for Perl and most shells. –  Blrfl Nov 27 '12 at 19:22

6 Answers 6

up vote 14 down vote accepted

The other answers are correct in that it makes no technical difference, but I have seen one informal style rule on a couple of open-source projects: double quotes are used for strings that might eventually be visible to the user (whether or not they need translation), and single quotes are for strings that relate to the functionality of the code itself (eg. dict keys, regular expressions, SQL).

This is certainly not a universal rule (or even codified in a PEP), so like any other arbitrary aspect of coding it comes down to local rules.

share|improve this answer

From a technical perspective it makes no difference, from a style perspective a few thoughts:

  • Most other programming languages use double quotes.
  • When writing in English usually double quotes are used.
  • Single quotes are useful when you have a lot non-technical/narrative text in your application because they allow you to use double quotes in a more natural way. bar = '"It\'s awesome" he said.'
  • On the other side the single quote character is also used in a few occasions in the english language. bar = "\"It's awesome\" he said."
share|improve this answer
    
Technically, your 4th point refers to a conjunction; the joining of the words "it" and "is". Just a note, though you did use it correctly: this is the only time an apostrophe should be used when writing "it's" - never for possession. –  Mike Partridge Jul 2 '12 at 13:44

There is no difference between using single quotes and double quotes in Python. In practical terms, depending on the kind of data you are working with, you may find one or the other style more convenient.

The type of quotes used "by default" (where there is no compelling reason to use one or the other) is pretty much a matter of personal preference.

For more information, see the section on String Literals in the Python documentation.

share|improve this answer

It is very simple:

  • Single quotes allow un-escaped embedding of double quotes.
  • Double quotes allow for embedding of un-escaped single quotes.

It is most Pythonic to use ' ( single quotes ) until you need double quotes.

The following quote is directly from the documentation on String literals.

In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences. A prefix of 'u' or 'U' makes the string a Unicode string. Unicode strings use the Unicode character set as defined by the Unicode Consortium and ISO 10646. Some additional escape sequences, described below, are available in Unicode strings. A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A “quote” is the character used to open the string, i.e. either ' or ".)

share|improve this answer
    
The question of course is - why is it Pythonic to prefer single quotes when almost every other language uses double quotes? –  Martin Beckett Jul 2 '12 at 4:03
2  
@MartinBeckett because –  Jarrod Roberson Jul 2 '12 at 4:58
    
Ahh of course ;-) –  Martin Beckett Jul 2 '12 at 5:11
    
@JarrodRoberson: I think "because" (or 'because', if you will) is a valid answer if it's written in some PEP (or similar) that it is the Pythonic way. Otherwise it's just personal choice. –  Joachim Sauer Jul 2 '12 at 7:41

In my opinion I prefer to use the double quotes for a string.

Why? For programmers of other languages ​​can be adapted quickly and understand the code more easily.

If a C programmer see:

'Writing "It\'s big?" No.'

Will be hesitant, instead of:

"Writing \"It's big?\" No."
share|improve this answer
3  
"Writing \"It's big?\" No." –  API-Beast Jul 2 '12 at 2:21
    
@Lucio That's what I've been doing, I just didn't know if I was violating some sort of established Python standard or not. –  Eric Hydrick Jul 2 '12 at 12:14
    
@EricHydrick No, you can choose for one or other. –  Lucio Jul 2 '12 at 22:43

Some things to consider when developing your habit of whether to use single or double quotes regularly.

Double quotes requires two finger keying; and therefore more likely for typing errors as well as a speed decrease

Your editor may show one character better then another; for easier visual syntax debugging.

Docstrings use triple characters: Which looks better? """""" or ''''''

Although I have not seen this on Windows OS, some syntax or documentation tools may expect double verse single quotes.

For most of the Python tutorials I've seen I see the opposite of your experience. I usually see double quotes used.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.