Home  >  Article  >  Backend Development  >  The difference between python single quotes, double quotes, and triple quotes

The difference between python single quotes, double quotes, and triple quotes

藏色散人
藏色散人Original
2019-07-03 09:19:0111223browse

The difference between python single quotes, double quotes, and triple quotes

The difference between single quotes, double quotes, and triple quotes in python

1 Common usage of single quotes and double quotes It is the same as the use of double quotes to represent string literals in C language.

eg:

(1) str1 = 'aaa' (等价于str = "aaa")
  (2) str2 = 'aaa,\
  bbb'
  (等价于str2 = "aaa,\
  bbb")

When printing str1 and str2, they are

  aaa
  aaa,bbb

2 respectively. The difference between quotation marks and double quotation marks is mainly reflected in the fact that when the string enclosed in single quotation marks contains ", there is no need to use the escape character (\), and vice versa

  (1) str1 = 'aaa"bbb'
  (2) str2 = "aaa'bbb"

Print str1, str2 respectively Yes

  aaa"bbb
  aaa'bbb

3 There are two forms of triple quotation marks ("""String content""", or '''String content''')

 f35d6e602fd7d0f0edfa6f7d103c1b57These two There is almost no difference in the usage of the form, but strictly speaking, there is the same difference as above

   str1 = '''aaa"""bbb'''(等价于"""aaa\"""bbb""")
   str2 = """aaa'''bbb"""(等价于'''aaa\'''bbb''')

 2cc198a1d5eb0d3eb508d858c9f5cbdbThe core usage of triple quotation marks is mainly reflected in cross-line strings, which will contain two delimiters All characters between symbols, including visible and invisible, such as carriage return and line feed characters

  str1 = '''aaa
  bbb'''

Print str1

  aaa
  bbb

Based on this feature of triple quotes, when formatting is required When entering multi-line characters, you can reduce the input of escape characters.

 5bdf4c78156c7953567bb5a0aef2fc53Also, there are no multi-line comment symbols in python, so triple quotes are generally used instead

Related recommendations:《 Python Tutorial

The above is the detailed content of The difference between python single quotes, double quotes, and triple quotes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn