QUOTATIONS AND COMMENTS IN PYTHON
Python allows single ('), double ("), and triple (''' or """) quotation marks for string literals, provided that they match at the beginning and the end of the string. You can use triple quotation marks for strings that span multiple lines. The following examples are legal Python strings:
word = 'word' line = "This is a sentence." para = """This is a paragraph. This paragraph contains more than one sentence."""
A string literal that begins with the letter “r” (for “raw”) treats everything as a literal character and “escapes” the meaning of meta characters, as shown here:
a1 = r'\n'
a2 = r'\r'
a3 = r'\t'
print('a1:',a1,'a2:',a2,'a3:',a3)
The output of the preceding code block is here:
a1: \n a2: \r a3: \t
You can embed a single quotation mark in a pair of double quotation marks (and vice versa) to display a single quotation mark or double quotation marks. Another way to accomplish the same result is to precede single or double quotation marks with a backslash (“\”) character. The following code block illustrates these techniques:
b1 = "'"
b2 = '"'
b3 = '\''
b4 = "\""
print('b1:',b1,'b2:',b2)
print('b3:',b3,'b4:',b4)
The output of the preceding code block is here:
b1: ' b2: " b3: ' b4: "
A hash sign (#) that is not inside a string literal is the character that indicates the beginning of a comment. Moreover, all characters after the # and up to the physical line end are part of the comment (and ignored by the Python interpreter). Consider the following code block:
#!/usr/bin/python
# First comment
print("Hello, Python!") # second comment
This will produce following result:
Hello, Python!
A comment may be on the same line after a statement or expression:
name = "Tom Jones" # This is also comment
You can comment multiple lines as follows:
# This is comment one # This is comment two # This is comment three
A blank line in Python is a line containing only whitespace, a comment, or both.