Python 中不同型別的引號是什麼?
在Python中,有不同型別的引號。每種引號根據需要用於不同的場景。以下是Python程式語言中可以使用不同型別的引號。
單引號
雙引號
三引號
什麼是單引號?
單引號 (' ') 用於在Python程式設計中建立字串和字元。由於print()函式接受字串值,因此我們可以直接將值傳遞給它,並將其用單引號括起來。
示例
在這個例子中,我們將使用單引號建立一個字串,並將其賦值給變數,然後列印輸出。
s = 'Welcome to Tutorialspoint' print(s) print(type(s))
輸出
Welcome to Tutorialspoint <class 'str'>
示例
在這裡,我們將字串值直接傳遞給print()函式:
print('Python Programming Language')
輸出
Python Programming Language
什麼是雙引號?
我們也可以使用雙引號(" ")來建立字串和字元。單引號 (')和雙引號 (")的功能相同;您可以根據需要使用其中一個。
示例
在這個例子中,我們使用雙引號建立一個字串值。
st = "Double quotes in python programming" print(st) print(type(st))
輸出
Double quotes in python programming <class 'str'>
示例
如果我們想列印一個語句,我們也可以直接將所需的文字用雙引號括起來,傳遞給print()函式。
print("Double quotes used in print statement")
輸出
Double quotes used in print statement
什麼是三引號?
三引號用於註釋和表示Python中的docString。
示例
在程式碼的一部分中,如果我們想要使用不屬於程式碼行的句子,那麼我們將這些句子放在三引號中。以下是程式碼。
string = "Hello world" '''The triple quotes are mainly used for commenting the lines in python programming language''' print(string)
輸出
以下是用於註釋程式碼行的三引號的輸出。在輸出中,我們可以看到三引號中給出的行不會顯示在輸出中。
Hello world
示例
除了三單引號,我們也可以使用三雙引號來註釋程式碼中的行。
s = "Hello world" """The triple quotes are mainly used for commenting the lines in python programming language""" print(s)
輸出
Hello world
廣告