osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strings: double versus single quotes


On 24/05/20 4:03 AM, Tim Chase wrote:
> On 2020-05-24 01:40, Chris Angelico wrote:
>> On Sat, May 23, 2020 at 10:52 PM Abdur-Rahmaan Janhangeer
>> <arj.python at gmail.com> wrote:
>>>
>>> The interpreter prefers single-quotes
>>>   
>>>>>> "single or double"
>>> 'single or double'
>>>   
>>>>> 'not all that strongly, it doesn\'t'
>> "not all that strongly, it doesn't"
> 
> But when a string contains both, it biases towards single quotes:
> 
>     >>> "You said \"No it doesn't\""
>     'You said "No it doesn\'t"'
> 
> ;-)


I'm highly amused by this interchange - for two reasons: that you'd 
noticed these details, and that I hadn't!

Yes, I'd noted that the REPL responds with single-quotes, even if I used 
double-quotes in the definition (per "single of double", above) - for 
whatever that's worth (it's not as if the delimiters are stored along 
with other variable detail!)

That the REPL is 'intelligent' about its choice to vary the delimiter to 
suit the content is good (design) sense - the best presentation (UX), ie 
doesn't is a better presentation than doesn\'t!

Which brings me to my own, humble, and quite often non-PEP-8-respecting 
habits: that I (basically) apply the ?rules of English grammar to Python 
(which I write (mostly) in English). Thus a "quotation", eg someone's 
name, but any piece of data, will be enclosed in double-quotes:

	name = "Abdur-Rahman"

whereas, when something will be 'calculated', I use singles, eg

	f'{ name } is top of the class'

Never learning a language which distinguishes single character literals 
from strings, eg the C examples quoted earlier, such 
thoughts/distinctions don't 'exist'.
(which makes me wonder why code reviews have never queried the point, or 
registered that I might seem to be behaving inconsistently...)
-- 
Regards =dn