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

藏色散人
Release: 2019-07-03 15:57:10
Original
11216 people have browsed it

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")
Copy after login

When printing str1 and str2, they are

  aaa
  aaa,bbb
Copy after login

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"
Copy after login

Print str1, str2 respectively Yes

  aaa"bbb
  aaa'bbb
Copy after login

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

 <1>These two There is almost no difference in the usage of the form, but strictly speaking, there is the same difference as above

   str1 = &#39;&#39;&#39;aaa"""bbb&#39;&#39;&#39;(等价于"""aaa\"""bbb""")
   str2 = """aaa&#39;&#39;&#39;bbb"""(等价于&#39;&#39;&#39;aaa\&#39;&#39;&#39;bbb&#39;&#39;&#39;)
Copy after login

 <2>The 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 = &#39;&#39;&#39;aaa
  bbb&#39;&#39;&#39;
Copy after login

Print str1

  aaa
  bbb
Copy after login

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

 <3>Also, 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template