目錄python
表示名字、愛好code
字符串至關於一根羊肉串。而字符串就是一串被串起來的字符,在單引號、雙引號或三引號內包裹的一串字符。須要注意的是:三引號內的字符能夠換行,而單雙引號內的字符不行。字符串
name1 = 'nick' name2 = "egon" print(id(name1)) print(type(name1)) print(name1)
4418849624 <class 'str'> nick
name3 = """nick egon""" print(name3)
nick egon
字符串只能+、*和邏輯比較it
字符串的拼接,即從新申請一個小空間把兩個字符串都拷貝一份後再拼接。而不是你YY的把一個小空間內的變量值複製到另外一個變量的小空間內,而後拼接。class
msg2 = "my name is 'nick'" msg3 = 'my name is "nick"' print(msg2 + msg3) # my name is 'nick'my name is "nick"
my name is 'nick'my name is "nick"
注意:若是字符串內有引號,則包裹字符串的引號和字符串內部的引號不能相同。變量
name = 'nick ' print(name * 10)
nick nick nick nick nick nick nick nick nick nick
注意:字符串的乘法只能乘以數字。di
msg1 = 'hello' msg2 = 'z' print(msg1 > msg2)
False
注意:字符串比較大小,按照ASCII碼比較,之後會細講。co
msg3 = 'zero' msg4 = 'zx' print(msg3 > msg4) print('Z' > 'A') print('Z' > 'a')
False True False
注意:字符串的比較是字母的順序。字符