DEV Community

David Armendáriz
David Armendáriz

Posted on

Enforcing single quotes in Python

So, I have tried black, yapf and autopep8 because those are the options that VS Code prompts me to use when I want to format on save.

Out of the 3, I find black the most useful, because it is more similar to Prettier. However, I don't like that it enforces me to use double quotes instead of single quotes. I have read in lots of places that this is not possible. I really like black but I find this limitation a HUGE disadvantage.

Do you know a way to enforce single quotes with black or do you prefer yapf or autopep8? Which one is more "flexible"? Do you use any of these formatters along with pylint?

Thanks for your answers!

Top comments (2)

Collapse
 
rhymes profile image
rhymes

I use black and I'm fine with the double quotes. From their documentation:

Why settle on double quotes? They anticipate apostrophes in English text. They match the docstring standard described in PEP 257. An empty string in double quotes ("") is impossible to confuse with a one double-quote regardless of fonts and syntax highlighting used. On top of this, double quotes for strings are consistent with C which Python interacts a lot with.

On certain keyboard layouts like US English, typing single quotes is a bit easier than double quotes. The latter requires use of the Shift key. My recommendation here is to keep using whatever is faster to type and let Black handle the transformation.

from github.com/psf/black/blob/master/d...

they explain there's a way not to enforce quotes normalization using --skip-string-normalization but I've never tried it.

You can also use it with flake8 if you want to go further in syntax checking: black.readthedocs.io/en/stable/com...

My recommendation is to surrender to the double quotes :D

Collapse
 
wojtow profile image
David Wojtowicz

I am glad to have discovered that black automatically standardizes quoting. At some point in my early learning of python I was under the impression that single quotes were preferred and consciously tried to use them. But I found myself doing so very inconsistently since double quotes come more naturally (being the required or preferred choice in most other of the dozen languages that I use regularly)

Black is self-admittedly very opinionated and there's a number of other things layout things that I just don't like either. But I finally decided to stop caring.

And that really have been very freeing. Now I just don't care about formatting how I think it looks pretty. I just let black reformat stuff in a highly consistent manner and focus on writing the code, not the presentation of the code (which I used to expend way too much mental energy and keystrokes on).