Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
I 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;
Triple 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 Box1,636 Points
As 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. >>>
Francesc Box1,636 Points
gender complaint code? :-) actually, copy/paste did not work, so I directly wrote the code on the window.
Rouillie Wilkerson10,407 Points
The 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!