Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Refinement

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Single or Double quotes?

Throughout the building of the apps in this course, especially the "Letter Game App", Kenneth Love alternates between single and double quotes with no discernible pattern that I can see. Is there a convention in Python that states when and where each are used, or is it just preference and whatever you happen to type?

3 Answers

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Thanks for the link Jeffrey. :smiley:

I figured it didn't matter technically, I'm just used to other languages that 'politely' ask you to choose one and stay consistent, and I'm okay with that... I personally like double quotes, but I was confused when Kenneth Love would change from one to the other and then back again.

One of the answers in that thread you posted stated that when the user may see the string, use doubles, otherwise single. Kenneth seems to follow that, but there are times where it goes backwards or random. Most times his print() statements use double quotes, but then sometimes it uses single. That's why I thought maybe Python had a specific style that is common among coders that I just couldn't see.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

A long time ago I was introduced to using single quotes for internal strings and double quotes for things that might be shown externally. It's an old habit, and one that I lean into without thinking too much about it.

By default I'd recommend favoring the use of single quotes. You'll find that in most Python Open Source code. Again stick with whatever is in play.

And +1 to what Jeffrey Austin said about the triple double quotes for docstrings, as well as his advice of making the string work without the need to escape it.

Jeffery Austin
Jeffery Austin
8,128 Points

It looks like you are right, python.org recommends picking one and staying consistent, found this on python.org https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds, great link that also mentions other best practices.

String Quotes

In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.

For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257 .