Tuesday, March 6, 2018

Triple Quotes


Triple Quotes in Python 3


Python's triple quotes comes to the rescue by allowing strings to span multiple lines,including verbatim NEWLINEs, TABs, and any other special characters.

The syntax for triple quotes consists of three consecutive single or double quotes.


When the above code is executed, it produces the following result. Note how every single special character has been converted to its printed form, right down to the last NEWLINE at the end of the string between the "up." and closing triple quotes. Also note that NEWLINEs occur either with an explicit carriage return at the end of a line or its escape code (\n) –


Raw strings do not treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it-


#!/usr/bin/python3
print ('C:\\nowhere')

When the above code is executed, it produces the following result-

C:\nowhere

Now let us make use of raw string. We would put expression in r'expression' as follows-

#!/usr/bin/python3

print (r'C:\\nowhere')

When the above code is executed, it produces the following result-

C:\\nowhere