在python中字符串可以用雙引號表示,也可以用單引號表示:
str1 = 'hello world'
str2 = "hello world"
這兩種字符串的表示方法沒有區別。
python雙引號和單引號定義字符串的好處:
如果字符串中有單引號,在使用單引號定義字符串時,字符串中間的單引號必須用轉移字符\才可以:
str3 = 'I\'m a big big girl'
此時我們使用雙引號表示字符串會更加簡潔明了:
str4 = "I'm a big big girl"
如果字符串中有雙引號,在使用雙引號定義字符串時,字符串中間的雙引號也必須用轉移字符\才可以:
str5 = "I like the word\"move on\",it means progress"
此時我們使用單引號表示字符串引號會更加簡潔明了:
str6 = 'I like the word"move on",it means progress'
三引號可以是單引號,也可以是雙引號。有兩個作用:一、字符串在不加\n的情況下實現多行輸出;二、加多行注釋
實現多行輸出(也可以寫成3個單引號,沒有任何區別):
str4 = """
hello world!
hello world!
hello world!
"""
多行注釋(也可以寫成3個單引號,沒有任何區別):
""
注釋1
注釋2
注釋3
"""