When to use single quotes and double quotes

722 views
Skip to first unread message

Zach Borboa

unread,
Oct 27, 2013, 5:18:34 PM10/27/13
to django-d...@googlegroups.com
I'm seeing a mix of both singly- and doubly-quoted strings in django source.
Other than docstrings wrapped in triple-double-quotes, when is it appropriate
to use double quotes instead single quotes?

Daniele Procida

unread,
Oct 27, 2013, 5:22:42 PM10/27/13
to Django Developers
As far as I understand it doesn't make any difference in Python, but apart from cases where what you use is dictated by the context (for example, a string with an apostrophe in it needs double quotes) I've wondered the same myself about what's preferred in Django.

Should our documentation examples prefer one rather over the other?

Daniele

Zach Borboa

unread,
Oct 27, 2013, 5:28:13 PM10/27/13
to django-d...@googlegroups.com
I believe we should prefer one over the other. That being said, legibility, consistency, and context should be taken into account.
Message has been deleted

Zach Borboa

unread,
Oct 28, 2013, 11:31:55 PM10/28/13
to django-d...@googlegroups.com

Here's an example from the django source where we are using double quotes in python instead of single quotes. The following code prints out html using single quotes instead of the double quotes regularly seen in html.

    return format_html("<input type='hidden' name='csrfmiddlewaretoken' value='{0}' />", csrf_token)

instead of:

    return format_html('<input type="hidden" name="csrfmiddlewaretoken" value="{0}" />', csrf_token)


This message inadvertently got deleted. If it was supposed to be removed. My apologies; feel free to again remove it.

Andres Osinski

unread,
Oct 28, 2013, 11:41:12 PM10/28/13
to django-d...@googlegroups.com
I don't know where I got this from, but I consider it to be the best
convention for choosing quote types:

Use double quotes for strings meant for human consumption, use single
quotes for everything else.

Text, trans strings, model verbose_names: double quotes
Dict keys, HTML tags, flags, and similar: single quotes

Use triple quotes for long blocks of text, ideally only necessary for
human-readable strings.
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-develop...@googlegroups.com.
> To post to this group, send email to django-d...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/ca45bbf4-5167-4f6b-ad3b-517957e10ced%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.



--
Andrés Osinski
http://www.andresosinski.com.ar/

Aymeric Augustin

unread,
Oct 29, 2013, 3:12:13 AM10/29/13
to django-d...@googlegroups.com
On 29 oct. 2013, at 04:41, Andres Osinski <andres....@gmail.com> wrote:

> Use double quotes for strings meant for human consumption, use single
> quotes for everything else.

I’m usually doing that in new code.

I don’t think it’s worth retrofitting the entire Django codebase, though.

--
Aymeric.




German Larrain

unread,
Nov 3, 2013, 12:18:43 AM11/3/13
to django-d...@googlegroups.com

Use double quotes for strings meant for human consumption, use single
quotes for everything else.

Text, trans strings, model verbose_names: double quotes
Dict keys, HTML tags, flags, and similar: single quotes

Use triple quotes for long blocks of text, ideally only necessary for
human-readable strings.


I had somewhat the same idea but your rule-of-thumb makes it way easier to remember! Thanks
Reply all
Reply to author
Forward
0 new messages