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) Python for Beginners Errors

Andrew Forryan
Andrew Forryan
811 Points

' or " in a string?

In previous videos you wrote a string using " e.g ("Hello World") whereas in this video you used ('Hello World'). Does it matter which one you use or do they both work the same way?

3 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points

Hey Andrew, in python there is no difference between double quoted strings and single quoted strings. I personally like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple quotes for docstrings.

person = {
'name': "Anish Walawalkar", 
'school': "University of Waterloo"
} 

def log(name='John Doe'):
    '''prints a greeting message to the screen'''
   print("Hello {}! How are you today?".format(name))

I may be super late on this, but Anish (or whoever uses the same methodology as him), why do you use different punctuation for those?

You mention that it doesn't matter, but is it a best practice to do something like:

print("Hello World!)
'''prints Hello World! to the screen'''

'Hello' + 'World'
'''Puts hello and world into 1 word'''

speaking of Docstrings, can you use "#" in the same way you would bash? or is that not good or doable for python? Also since ' and " do the same thing, would ''' and """ do the same?

Please excuse my poor examples, I am taking this course for a reason! =]

In Python 2, you can write your string either way (within parenthesis or not). In Python 3, the parentheses are required.

Take a look at this post from the Treehouse blog, specifically the section titled "Print is a Function Like It Should Have Been All Along".

Andrew Forryan
Andrew Forryan
811 Points

Ok, thanks for the help.