Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triple quotation in python

Tags:

python

syntax

So I understand that if I do the following

print """ Anything I 
          type in here 
          works. Multiple LINES woohoo!"""

But what if following is my python script

""" This is my python Script. Just this much """

What does the above thing do? Is it taken as comment? Why is it not a syntax error?

Similarly, if I do

"This is my Python Script. Just this. Even with single quotes."

How are the above two scripts interpreted?

Thanks

like image 609
Kraken Avatar asked Apr 29 '14 09:04

Kraken


1 Answers

The triple quotes ''' or """ are just different ways of representing strings. The advantage of triple quotes is that it can span multiple lines and sometimes serve as docstrings.

The reason:

"hadfasdfas"

doesn't raise any error is because python simply creates the string and then doesn't assign it to anything. For the python interpreter, it is perfectly fine if you have a pointless statement in your code as long as there are no syntax or semantics errors

Hope that helps.

like image 164
sshashank124 Avatar answered Oct 30 '22 15:10

sshashank124