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 trialJason Anders
Treehouse Moderator 145,860 PointsSingle 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
Jeffery Austin
8,128 PointsI think it is a matter of preference. Here is a link that explains more: http://programmers.stackexchange.com/questions/155176/single-quotes-vs-double-quotes
Craig Dennis
Treehouse TeacherA 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.
Jason Anders
Treehouse Moderator 145,860 PointsAwesome. Thanks Craig Dennis
Jeffery Austin
8,128 PointsIt 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 .
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsThanks for the link Jeffrey.
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.