87

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.

4
  • 5
    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, 2012 at 13:09
  • 6
    The same is true for Perl and most shells.
    – Blrfl
    Nov 27, 2012 at 19:22
  • 19
    How is this question "not constructive'? As an absolute python beginner, I need to know this. If you write bash script, there is a big difference. Oct 10, 2016 at 8:53
  • 20
    Not constructive? Geez. The first google result for "python single double quotes", tells you what you need to know quickly. Thanks for asking Eric and answering detly Nov 14, 2016 at 18:11

6 Answers 6

70

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.

Note that PEP 8 (which I hadn't noticed when I wrote this answer) says:

This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.

As a commenter points out, this isn't necessarily contradictory, depending on how you interpret "rule". What I suggest doesn't really work with the second half of the quote though.

5
  • 1
    This may be house style, but it is against PEP 8: pick one and use it for the whole file, regardless of content. Jan 3, 2019 at 15:54
  • 5
    @NilsvonBarth The PEP says "pick a rule and stick to it", not "pick one delimiting character and stick with it". "Double quotes for user-visible text, single quotes for things used by the machine" seems like a perfectly fine rule to stick to. Jan 4, 2019 at 20:53
  • 1
    @Tanner Swett Sorry, you're right; it's the Google Python style guide that specifies “within a file” google.github.io/styleguide/pyguide.html#310-strings Jan 6, 2019 at 20:42
  • Black actually formats Python strings to be surrounded by double quotes. Even jslint wants JavaSciprt strings to be surrounded by double quotes. Most older languages never even provided options. Just because you can do it doesn't mean you should. Of course, there a few instances where this is a very nice thing to have, like when you have a quote in the string. Practically speaking, though, most of your strings should come from a data set that is not hard-coded in your code base.
    – Bobort
    Apr 18, 2019 at 17:44
  • 1
    @bobort Even if you're looking strings up in a translation or UI system, you often still need string keys for those in your code. But I agree that things have moved on, and there's not really any benefit to this distinction.
    – detly
    Apr 19, 2019 at 0:44
24

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

5
  • 18
    The question of course is - why is it Pythonic to prefer single quotes when almost every other language uses double quotes? Jul 2, 2012 at 4:03
  • 15
    @MartinBeckett because
    – user7519
    Jul 2, 2012 at 4:58
  • 3
    Ahh of course ;-) Jul 2, 2012 at 5:11
  • 7
    @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. Jul 2, 2012 at 7:41
  • 8
    single quotes look cleaner and also don't need a shift key to type
    – JoelFan
    May 29, 2017 at 4:23
17

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."
1
  • Note that " is often used in technical strings, too -e,g f'you submitted "{dumb_thing} but that is too dumb even for us!' Jan 7, 2019 at 12:36
4

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.

4

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.

4

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."
3
  • 3
    "Writing \"It's big?\" No."
    – API-Beast
    Jul 2, 2012 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. Jul 2, 2012 at 12:14
  • @EricHydrick No, you can choose for one or other.
    – Lucio
    Jul 2, 2012 at 22:43

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