Python: The difference between single quotes' double quotes "triple quotes''' and """

In python, single quotes and double quotes have the same functions, both can represent strings, and both can play the function of escaping.

E.g:

print('-\t-\\-\'-%-/-\n')
print("-\t-\\-\'-%-/-\n")

Their display results are the same: both display the escaped characters:

-    -\-'-%-/-

In fact, in terms of escaping, the functions of the three quotation marks are also the same, for example:

print('''-\t-\\-\'-%-/-\n''')
print("""-\t-\\-\'-%-/-\n""")

The above results will also be displayed.

However, the uniqueness of triple quotation marks is here: multiple lines can be displayed.
For example:

# 三单引号(''')
print('''i
love
you''')

# 三双引号(""")
print(""" I can print ''' """)
print("""i
love
you""") 

The printing effect of the code at both ends of the above is the same, both show:

i
love
you

So the summary is: python single quotation mark, double quotation mark, triple quotation mark''' and """ are both strings and can be escaped. The only difference is that triple quotation marks''' and """ can display multiple lines.

Unescaped string

The above representations are all escaped, so how to achieve unescaped representation characters.

Just add an r before the string.

# 不转义
print(r'\n')

结果:
\n

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/86289242