Strings: double versus single quotes

Cameron Simpson cs at cskk.id.au
Tue May 19 20:54:14 EDT 2020


On 19May2020 22:04, Manfred Lotz <ml_news at posteo.de> wrote:
>On Tue, 19 May 2020 13:36:54 -0500
>Tim Chase <python.list at tim.thechases.com> wrote:
>> On 2020-05-19 20:10, Manfred Lotz wrote:
>> > I am asking myself if I should preferably use single or double
>> > quotes for strings?
>>
>> I'd say your consistency matters more than which one you choose.
[...]
>> I personally use habits from my C days:  double-quotes for everything
>> except single characters for which I use a single-quote:
>>
>>   if 'e' in "hello":
>>
>> as in indicator that I'm using it as a single character rather than
>> as a string.
>>
>> I don't have a firm rule for myself if a string contains
>> double-quotes.  It doesn't happen often for me in a case where I
>> couldn't use a triple-quoted string or that I am refering to it as a
>> single character. [...]
>
>I am influenced by Perl which I used before. In Perl I used double
>quoted strings when I wanted interpolation. Otherwise single quoted
>strings. In Rust (or C) it is double quotes for strings and single
>quotes for characters.

And I, the shell (where single quotes are "raw" and double quotes allow 
parameter substitution).  So I use single quotes for plain old strings 
and double quotes when I'm going to be formatting the string with % 
(therefore, in most logging calls).

Personally I find double quotes visually noisy, and prefer single 
quotes. Most of the time.

This is one of the reasons I autoformat with yapf (which doesn't change 
my quoting) rather than black (which is very opinionated and also 
untunable, and _does_ change my quotes).

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list