coding style – Single quotes vs. double quotes in Python

coding style – Single quotes vs. double quotes in Python

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 arent needed.

For example:

    English: There are %(number_of_lights)s lights.,
    Pirate:  Arr! Thar 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|avast|yohoho)!, message) is not None

Quoting the official docs at

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 youll only end up confusing any newcomers.

coding style – Single quotes vs. double quotes in Python

I used to prefer , especially for docstrings, as I find this creates some fluff. Also, can be typed without the Shift key on my Swiss German keyboard.

I have since changed to using triple quotes for docstrings, to conform to PEP 257.

Leave a Reply

Your email address will not be published. Required fields are marked *