重新梳理Python基礎(2)

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.