python基礎學習之days1總結

來源:互聯網
上載者:User

標籤:other   結果   引號   span   The   int   int()   填充   code   

一、逸出字元的使用

在實際應用中,我們可能要引用別人的話或使用英文縮寫,這種時候,在print()函數中,要想將引號作為字元列印出來,就得用到逸出字元 “ \ ”了。

例如,當我們列印 He says, "Forget the memories,continue to be life,miss,just pass by."時,可以這樣做

>>>print("He says, \"Forget the memories,continue to be life,miss,just pass by.\"")
#反斜線\在引號前面,將雙引號作為一個字元

另外,還可以這樣做

>>>print(‘He says, "Forget the memories,continue to be life,miss,just pass by."‘)
#當要列印的語句裡面有引號時,可以用與之不同的引號來作為指示符,這樣也能起到同樣的效果
>>>print(‘Wise men learn by other men\‘s mistakes,fools by their own‘)#通過在men‘s中的‘前加上反斜線\,使得python認為是一個字元,這樣就可以完美的解決了Wise men learn by other men‘s mistakes,fools by their own

二、串聯多個字串

串聯多個字串是,使用 “+”號。但是如果想要輸出的結果有間隔,需要增加空白字元串

>>>print(‘alex‘ + ‘ ‘ + ‘ammy‘)alex ammy

當然,如果是使用print()函數,可以直接使用逗號來將字串隔開即可:

>>>print(‘alex‘,‘ammy‘,‘mike‘)alex ammy mike

此外,如果我們想自訂輸出格式,可以使用格式說明符 “%”,並可以在“%”後面添加表示長度的數值,例如

>>>print("%s %s %10s" % (‘john‘,‘mike‘,‘man‘) )john mike        man
#10表示一個長度為10的字串,第三個字元man佔了3位,那麼就會填充7個空格

>>>print("%f" % (43.5-36.1))
7.400000

>>>print("%.2f" % (43.5-36.1))

7.40


如果想要列印"%",可在"%"前面再添加一個"%"。

 

python基礎學習之days1總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.