|
|
Subscribe / Log in / New account

Escape sequences in Python strings

Escape sequences in Python strings

Posted Aug 9, 2019 3:59 UTC (Fri) by da4089 (subscriber, #1195)
In reply to: Escape sequences in Python strings by flussence
Parent article: Escape sequences in Python strings

In my experience, supporting both single and double quotes for string literals is high-profile violation of the "there should be only one obvious way to do it" philosophy.

Regardless of the convenience of "'" or '"', the mental load of choosing a quote type for every string is real: I'm frequently looking at the rest of the file to check what the usual convention is in this specific code, or going back and converting a bunch of literals that I've written using the wrong quotes.

As an unrepentant C programmer, I'm inclined to use single quotes for characters and doubles for strings, and that just creates even more pain.

Maybe deprecating one or the other could be a Python4 feature? /jk, kinda


to post comments

Escape sequences in Python strings

Posted Aug 10, 2019 0:47 UTC (Sat) by mina86 (guest, #68442) [Link] (3 responses)

Simply use single quotes each time and the cognitive load is gone. As an added bonus, single quote is easier to type.

If you're willing to tolerate minor cognitive load, use double quotes if string contains single but not double quotes.

Finally, since Python doesn't have a character type, distinguishing between them and longer strings is counterproductive. Python is not C is not Algol.

Escape sequences in Python strings

Posted Aug 12, 2019 14:02 UTC (Mon) by Bluehorn (subscriber, #17484) [Link] (2 responses)

> As an added bonus, single quote is easier to type.

On an american keyboard, maybe. On a german qwertz keyboard, the double quote is above the digit 2 in the top row and the single quote is next to the big enter key. Which of course has (or maybe had) a different form on many keyboards which is why I prefer to use double quotes wherever possible.

Things are seldom simple in IT :-(

Greetings, Torsten

Escape sequences in Python strings

Posted Aug 12, 2019 14:19 UTC (Mon) by mina86 (guest, #68442) [Link]

No need to press shift. I still call it simpler to type. But I guess your millage may vary.

I’m using Programmer Dvorak anyway which has ‘ü’ easily accessible and that’s pretty much all German characters I need to type. ;)

Escape sequences in Python strings

Posted Aug 12, 2019 14:43 UTC (Mon) by mbunkus (subscriber, #87248) [Link]

Back in the day when I was writing a lot of LaTeX I had to switch from my native German keyboard layout to the English one. All of the important LaTeX characters such as the backslash and both pairs of curly & square brackets require an AltGr (right alt for all of you who don't know German keyboard layout) combination to type. After several years this caused a lot of pain both in my right thumb and my right wrist. I've continued using English layout (with custom bindinds for German Umalute & the ß) to this day, nearly twenty years later — and not only is LaTeX much easier on the joints, pretty much all of the programming languages I use regularly are.

If all you've ever done is using an English layout, count yourself lucky. A lot of us international users have it quite a bit harder.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds