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 trialJeremy Chae
117 PointsI don't quite understand what triple quotes are for in python.
I have watched this video so many times now and still cannot understand what triple quotes are for in python. The official Python Documentation says its for spanning across three lines for a string, but the video doesn't show that happen. HELP!!!
Please explain this python triple quotes in very easy language. I am just a beginner;
5 Answers
gsav
3,800 PointsTriple quotes are for setting a string that spans multiple lines. It is also the convention for writing Docstrings (ignore this if you haven't covered Docstrings yet).
It's also noteworthy that a triple quote will ignore single quotes allowing you to add them for grammar purposes.
Here's a couple examples:
'Example of a single quote spanning multiple
lines. It doesn't work!'
""" Example of triple quotes spanning
multiple lines.
It works
perfectly.
Also not the use of "quotations"
without having weird formatting issues.
"""
Francesc Box
1,637 PointsAs a complement to previous answer, it's handy when you want to work with a string that contains both single and double quotes at the same time. You will use triple quotes at the beginning and at the end of the string; then you are free to mix single and double quotes within the string.
>>> var = """ "I can't do it", he said. """
>>> print(var)
"I can't do it", she said.
>>>
imtrying
19,943 PointsFor some reason your output said she, instead of he.
var = """ "I can't do it", he said""" print(var) "I can't do it", he said
kristianburrows
2,283 PointsAre you referring to the "apostraphe" in (CAN'T) as the single quote?
Francesc Box
1,637 Pointsgender complaint code? :-) actually, copy/paste did not work, so I directly wrote the code on the window.
Podrig Leoghain
5,094 PointsThat's what she said.
Rouillie Wilkerson
10,419 PointsThe triple quotes are like very specific instructions to keep the quotes within the string, in addition to the initial opening and closing quotes.
For instance. If I want to write: I stared at the evil bunny, "halt!" it hissed.
I would code it as follows: """I stared at the evil bunny, "halt!" it hissed."""
Then I hit return, an this is what I'd see: 'I stared at the evil bunny, "halt!" it hissed.'
Notice how the halt is still in proper quotes? The triple quotes, at the start and finish told the program to keep those quotes around the word halt.
Now, if you enter print(_) at the prompt, it'll print as follows:
I stared at the evil bunny, "halt!" it hissed.
Just as you intended! I hope this helps!
gsav
3,800 Pointsgsav
3,800 PointsMoved my answer from a comment to an answer... Thanks for the heads up Steven.