python中format()函數的簡單使用教程,pythonformat

來源:互聯網
上載者:User

python中format()函數的簡單使用教程,pythonformat

先給大家介紹下python中format函數,在文章下面給大家介紹python.format()函數的簡單使用

---恢複內容開始---

python中format函數用於字串的格式化

通過關鍵字

 print('{名字}今天{動作}'.format(名字='陳某某',動作='拍視頻'))#通過關鍵字 grade = {'name' : '陳某某', 'fenshu': '59'}print('{name}電工考了{fenshu}'.format(**grade))#通過關鍵字,可用字典當關鍵字傳入值時,在字典前加**即可

通過位置

 print('{1}今天{0}'.format('拍視頻','陳某某'))#通過位置 print('{0}今天{1}'.format('陳某某','拍視頻'))

填充和對齊^<>分別表示置中、靠左對齊、靠右對齊,後面頻寬度

print('{:^14}'.format('陳某某'))print('{:>14}'.format('陳某某'))print('{:<14}'.format('陳某某')) print('{:*<14}'.format('陳某某')) print('{:&>14}'.format('陳某某'))#填充和對齊^<>分別表示置中、靠左對齊、靠右對齊,後面頻寬度

精度和類型f精度常和f一起使用

print('{:.1f}'.format(4.234324525254))print('{:.4f}'.format(4.1))

進位轉化,b o d x 分別表示二、八、十、十六進位

print('{:b}'.format(250))print('{:o}'.format(250))print('{:d}'.format(250))print('{:x}'.format(250))千分位分隔字元,這種情況只針對與數字print('{:,}'.format(100000000))print('{:,}'.format(235445.234235))

下面看下python中的format函數:

                            用於對字串的格式化

1.通過位置傳遞

print('{0}xxxxxxxx{1}'.format('這是0要傳入的東西','這是1要傳入的東西'))

2.通過關鍵字傳遞   

 print('{0}xxxxxxxx{1}'.format(0='xxx',1='xxx'))

    如果是字典     

 print('{keys}xxxxxxxx{values}'.format(**<字典名>))

3.充當數位千分位分隔字元(例如:1,000,000)

 print('{:,}'.format(1000000))

總結

以上所述是小編給大家介紹的python中format()函數的簡單使用教程,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

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