1. String類型輸出,看樣本
x = "There are %d types of people" % 10binary = "binary"do_not = "don't"y = "Those who know %s and those who %s" % (binary, do_not)z = "Those who know %r and those who %r" % (binary, do_not)print xprint yprint zprint "I said: %r." % xprint "I also said: '%s'" % yhilarious = Falsejoke_evaluation = "Isn't that joke so funny?! %r"print joke_evaluation % hilariousw = "This is the left side of..."e = "a string with a right side."print w + e
值得注意的是%r顯示一個字串是帶單引號的。
%r是用來debuging的,輸出未經處理資料,就是一個變數是什麼就輸出什麼
以下是輸出
There are 10 types of peopleThose who know binary and those who don'tThose who know 'binary' and those who "don't"I said: 'There are 10 types of people'.I also said: 'Those who know binary and those who don't'Isn't that joke so funny?! FalseThis is the left side of...a string with a right side.
2. print的時候,當這行末尾有逗號時,不會換行,例如
end1 = "C"end2 = "h"end3 = "e"end4 = "e"end5 = "s"end6 = "e"end7 = "B"end8 = "u"end9 = "r"end10 = "g"end11 = "e"end12 = "r"print end1 + end2 + end3 + end4 + end5 + end6, #如果沒逗號,會輸出兩行,如果有逗號,會輸出在同一行print end7 + end8 + end9 + end10 + end11 + end12
輸出
Cheese Burger
3. Escape Sequences
View Code
tabby_cat = "\tI am tabbed in"persian_cat = "I am split \non a line."backslash_cat = "I am \\ a \\ cat"fat_cat = """I'll do a list:\t* Cat food\t* Fishies\t* Catnip\n\t* Grass"""print tabby_catprint persian_catprint backslash_catprint fat_cat
while True: for i in ["/","-","|","\\","|"]: print "%s\r" % i, #\r表示退格
輸出結果
View Code
I am tabbed inI am spliton a line.I am \ a \ catI'll do a list: * Cat food * Fishies * Catnip * Grass