Python 中單引號和雙引號有什麼區別?
Python 使用引號來表示字串物件。它們可以是單引號或雙引號。這兩種方法都是正確的,並且工作方式相同;但是,當這些引號一起使用時,就會出現差異。
在本文中,我們將學習單引號和雙引號之間的區別。
Python 中的單引號
單引號應該用於在 Python 中包裝小而短的字串,例如字串字面量或識別符號。您必須記住,在用單引號表示字串的同時,使用單引號作為字串的一個字元將引發錯誤。在這種情況下,建議使用雙引號。讓我們透過一個例子來理解它。
示例
在以下示例中,我們將表示多種型別的字串:單個單詞、多個單詞和多個句子。
name = 'Rahul' print(name) channel = 'Better Data Science' print(channel) paragraph = 'Rahul enjoys the content on Better Data Science. Rahul is awesome. Think like Rahul.' print(paragraph) hybrid = 'Hello "World"!' print(hybrid)
輸出
如果我們執行上面的程式,則輸出如下所示:
Rahul Better Data Science Rahul enjoys the content on Better Data Science. Rahul is awesome. Think like Rahul. Hello "World"!
示例
讓我們看看下面另一個程式;在一個字串中使用多個單引號。
hybrid = 'Rahul's World' print(hybrid)
輸出
該程式引發瞭如下所示的語法錯誤:
File "/home/cg/root/96099/main.py", line 1
hybrid = 'Rahul's World'
^
SyntaxError: unterminated string literal (detected at line 1)
Python 假設字串在“Rahul”之後結束,因此之後的任何內容都是語法錯誤。在程式碼編輯器中,此類錯誤很容易識別,因為 We 之後的部分顏色不同。
解決此問題的方法有
停止使用縮寫(we are -> we're)——它們不方便。
跳脫字元串——這是我們接下來要研究的一個選項。
使用雙引號。
跳脫字元串
跳脫字元串的基本目標是防止在計算機語言中使用某些字元。例如,我們不希望撇號被視為引號。
示例
要在 Python 中跳脫字元串字元,請使用反斜槓 (\) 符號
hybrid = 'Rahul's World' print(hybrid)
輸出
上面此程式的輸出如下所示:
Rahul's World
示例
但是,反斜槓在字串中經常用作文字字元,例如表示計算機的路徑。讓我們看看如果您嘗試列印帶有跳脫字元的路徑會發生什麼。
print('C:\Users\John')
輸出
如果我們編譯並執行上面的程式,則會引發語法錯誤:
C:\Users\John C:\Users\John
可能不是您希望看到的。事實證明,有兩種方法可以避免跳脫字元:
如果您使用的是原始字串,請在第一個引號標記之前編寫“r”。
使用雙反斜槓有效地轉義跳脫字元。
以下是如何執行這兩者:
示例
#Write r before the first quote mark if you're using a raw string
print(r'C:\Users\John')
#Use a double backslash to effectively escape the escape character
print('C:\Users\John')
輸出
如果我們執行上面的程式,則輸出顯示如下:
C:\Users\John C:\Users\John
這兩條規則適用於用單引號和雙引號括起來的字串。現在,讓我們在本章的後面進一步討論在字串中使用雙引號。
Python 中的雙引號
雙引號推薦用於自然語言通訊、字串插值或當您知道字串中將包含單引號時。讓我們使用下面的示例更好地理解。
示例
讓我們在下面的示例中看看可以使用雙引號在 Python 中表示字串的各種情況。
name = 'Rahul'
# Natural language
print("It is easy for us to get confused with the single and double quotes in Python.")
# String interpolation
print(f"{name} said he is free today.")
# No need to escape a character
print("We're going out today.")
# Quotation inside a string
print("my favourite subject is 'maths'")
輸出
如果我們編譯並執行上面的程式,則輸出如下所示:
It is easy for us to get confused with the single and double quotes in Python. Rahul said he is free today. We're going out today. my favourite subject is 'maths'
如您所見,將引號嵌入到用雙引號括起來的字串中非常簡單。我們也不需要像使用單引號那樣跳脫字元。
示例
請記住,您不能在用雙引號括起來的字串中再次使用雙引號。這將導致與單引號相同的語法問題。讓我們在下面的示例中看看。
string = "He said, "I can't handle this anymore"." print(string)
輸出
上面程式的輸出如下所示:
File "/home/cg/root/22693/main.py", line 1
string = "He said, "I can't handle this anymore"."
^
SyntaxError: unterminated string literal (detected at line 1)
示例
為避免這種情況,您可以應用上一節的方法,但您可以改為用單引號括起字串:
string = 'He said, "I cannot handle this anymore".' print(string)
輸出
輸出如下所示:
He said, "I cannot handle this anymore".
結論
在 Python 中,單引號和雙引號字串之間的區別微不足道。只要遵循程式設計約定,您可以將其中任何一個用於任何目的。在某些情況下,一種型別比另一種型別更具優勢。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP