Wednesday, May 1, 2024
HomePythonSingle Or Double Quotes In Python?

Single Or Double Quotes In Python?


No matter you concentrate on Twitter today, it’s fairly useful for us builders to ask questions and get inputs 💡

Final week I requested about single vs double quotes in Python:

I assumed double quotes have been a given, black – “The Uncompromising Code Formatter”, defaults to them and I personally discover them nice to the attention.

Till I examine a distinct formatter in Fluent Python (2nd ed) known as blue (“a considerably much less uncompromising code formatter than black”) and its choice for single quotes:

Given the purpose of implementing a “commonplace” coding type, blue is best than black as a result of it follows Python’s personal type of utilizing single quotes by default, double quotes in its place … [truncated code example] … The choice for single quotes is embedded in repr(), amongst different locations in CPython. The doctest module is dependent upon repr() utilizing single quotes by default. One of many authors of blue is Barry Warsaw, coauthor of PEP 8, Python core developer since 1994, and a member of Python’s Steering Council from 2019 to current (July 2021). We’re in excellent firm after we select single quotes by default. Should you should use black, use the black -S possibility. Then it is going to go away your quotes as they’re.

Fluent Python (2nd version) by Luciano Ramalho

So now I’m torn, ought to I alter to single quotes?

Here’s a abstract of a number of the fascinating replies to my query on Twitter:

– Single. Nevertheless it relies upon from the context. + I don’t have a choice, both one is ok, so long as it’s constant alongside the entire file! 

That is nice level. As a lot as you wish to implement PEP8, requirements and thus the usage of single vs double quotes, in case you are tied to an current code base, you wish to desire consistency above anything.

– Single, however not sufficient that I really feel compelled to tweak/substitute black 🙂 

Yep utilizing black is a robust argument for utilizing double quotes. However due to Fluent Python I realized concerning the -S to go away quotes alone (or swap to blue, I but need to attempt it …)

– Single quotes are at all times my first selection since they require only one keystroke. 

That is certainly a bonus I’m experimenting with: how a lot does it velocity up the precise act of writing code? And because of auto-formatting you possibly can nonetheless write single quotes, but have them swapped out for double quotes in your remaining code 😮

– Double. Permits utilizing apostrophes in strings and makes the string assertion look extra express and remaining. And the code seems to be extra readable and professional general. Single-quotes scattered in every single place makes the code look messy prefer it has been quick-scripted collectively. It smells. + If it’s an f-string with quotes contained in the curly braces, these are double quotes as an alternative of escaped single quotes. 

The embedding of single quotes (and never having to flee them like ' which is kind of ugly) has been one in all my primary causes for me to stay with double quotes to date (other than habits being exhausting to interrupt 😅)

– Single quotes. The identical as Python prefers (in reprs). 

Following Python’s conventions is at all times a fantastic argument and one factor that stood out from the Fluent Python quote above.


I discovered it fascinating to a minimum of mirror a bit on this. Happily, due to having auto-formatters, you possibly can largely ignore this (“set and overlook“). For instance through the use of your favourite auto-formatter with a instrument like pre-commit (you possibly can see me set it up right here). So you should use no matter you want when writing code, to have it’s persistently auto-formatted when committing your code.

So long as we’re constant! I feel that’s the primary takeaway from this text.

Be at liberty to make use of talked about tweet so as to add your 2 cents …

Thanks for studying.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments