python格式符

來源:互聯網
上載者:User

標籤:表示   除了   orm   price   對齊   浮點數   無效   寬度   1.2   

‘‘‘字串除了用format()方法進行格式化外,還可以使用格式化運算式來處理字串。字串格式化運算式用%表示,%之前需要進行格式化的字串。字串格式化運算式用%表示,%之前為需要進行格式化的字串,%之後為需要填入字串中的實際參數。例如:%4.2f表示總長度為4,2位描述;‘‘‘s="The %s‘s price id %4.2f"%(‘apple‘,2.5)print(s)s=‘%s %s %s‘%(123,1.23,‘abc‘)print(s)s=‘%r %r %r‘%(123,1.23,‘abc‘)print(s)s=‘123%c%c‘%(‘a‘,97)print(s)s=‘%d%d‘%(123,1.56)print(s)d未指定寬度是,數字原樣轉換,%d會將浮點數轉換為整數。s=‘%6d‘%123print(s)指定寬度時,預設填充空格s=‘%-6d‘%123print(s)指定寬度時,同時靠左對齊;s=‘%06d‘%123print(s)%06d:指定寬度並填充0;s=‘%-06d‘%123print(s)同時使用靠左對齊和填充0,填充0 無效;ss=‘%+6d%+6d‘%(123,-123)print(ss)用+表示整數,-表示負數,預設填充空格。s=‘%+06d%+06d‘%(123,-123)print(s)用加減號表示加號或減號,指定填充0將整數轉換為八或十六進位格式控制o表示將整數轉換為八進位,x和X表示將整數轉換為十六進位。a=‘%o%o‘%(100,-100)print(a)

#指定寬度為8,八進位,將100轉換為8進位
s=‘%8o%8o‘%(100,-100)
print(s)
s=‘%x%X‘%(445,-445)
print(s)
s=‘%8x%8X‘%(445,-445) #長度為8
print(s)
s=‘%08x%08X‘%(445,-445)
print(s)
#指定字串寬度並填充為0;
s=‘%(name)s is %(age)d years old‘%{‘name‘:‘Tome‘,‘age‘:25}
print(s)

python格式符

聯繫我們

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