python中單引号和雙引号的區别?>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
,我來為大家講解一下關于python中單引号和雙引号的區别?跟着小編一起來看一看吧!
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
在交互式解釋器中,輸出字符串括在引号中,特殊字符用反斜杠轉義。雖然這有時看起來可能與輸入不同(括起來的引号可能會更改),但這兩個字符串是等效的。如果字符串包含單引号且不包含雙引号,則字符串放在雙引号中,否則用單引号括起來。 print()函數通過省略括起來的引号并打印轉義字符和特殊字符來生成更具可讀性的輸出:
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
如果不希望前面的字符被解釋為特殊字符,則可以通過在第一個引号前添加一個原始字符串:\r
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
字符串文本可以跨越多行。一種方法是使用三引号:或 。行尾會自動包含在字符串中,但可以通過在行尾添加 來防止這種情況發生。以下示例:"""...""" 和'''...'''
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
生成以下輸出(請注意,不包括初始換行符):
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
與其他語言不同,特殊字符單引号(‘ ’)和雙引号(“ ”)具有相同的含義。兩者之間的唯一區别是,在單引号中,您不需要轉義(如果你已經轉義),反之亦然。
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!