python format 用法详解
前序:format是python2.6新增的一個格式化字符串的方法,相對于老版的%格式方法,它有很多優(yōu)點。
format填充字符串
一 填充
1.通過位置來填充字符串
print('hello {0} i am {1}'.format('world','python')) # 輸入結(jié)果:hello world i am python print('hello {} i am {}'.format('world','python') ) #輸入結(jié)果:hello world i am python print('hello {0} i am {1} . a now language-- {1}'.format('world','python') # 輸出結(jié)果:hello world i am python . a now language-- pythonforamt會把參數(shù)按位置順序來填充到字符串中,第一個參數(shù)是0,然后1 ……
也可以不輸入數(shù)字,這樣也會按順序來填充
同一個參數(shù)可以填充多次,這個是format比%先進的地方
2.通過key來填充
obj = 'world' name = 'python' print('hello, {obj} ,i am {name}'.format(obj = obj,name = name)) # 輸入結(jié)果:hello, world ,i am python3.通過列表填充
list=['world','python'] print('hello {names[0]} i am {names[1]}'.format(names=list))# 輸出結(jié)果:hello world i am python print('hello {0[0]} i am {0[1]}'.format(list)) #輸出結(jié)果:hello world i am python4.通過字典填充
dict={‘obj’:’world’,’name’:’python’}
print(‘hello {names[obj]} i am {names[name]}’.format(names=dict)) # hello world i am python
注意訪問字典的key,不用引號的
5.通過類的屬性填充
class Names():obj='world'name='python'print('hello {names.obj} i am {names.name}'.format(names=Names))#輸入結(jié)果hello world i am python6.使用魔法參數(shù)
args = [‘,’,’inx’]
kwargs = {‘obj’: ‘world’, ‘name’: ‘python’}
print(‘hello {obj} {} i am {name}’.format(*args, **kwargs))#輸入結(jié)果:hello world , i am python
注意:魔法參數(shù)跟你函數(shù)中使用的性質(zhì)是一樣的:這里format(*args, **kwargs)) 等價于:format(‘,’,’inx’,obj = ‘world’,name = ‘python’)
format 格式轉(zhuǎn)換
| 3.1415926 | {:.2f} | 3.14 | 保留小數(shù)點后兩位 |
| 3.1415926 | {:+.2f} | 3.14 | 帶符號保留小數(shù)點后兩位 |
| -1 | {:+.2f} | -1 | 帶符號保留小數(shù)點后兩位 |
| 2.71828 | {:.0f} | 3 | 不帶小數(shù) |
| 1000000 | {:,} | 1,000,000 | 以逗號分隔的數(shù)字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00E+09 | 指數(shù)記法 |
| 25 | {0:b} | 11001 | 轉(zhuǎn)換成二進制 |
| 25 | {0:d} | 25 | 轉(zhuǎn)換成十進制 |
| 25 | {0:o} | 31 | 轉(zhuǎn)換成八進制 |
| 25 | {0:x} | 19 | 轉(zhuǎn)換成十六進制 |
| 5 | {:0>2} | 05 | 數(shù)字補零(填充左邊, 寬度為2) |
| 5 | {:x<4} | 5xxx | 數(shù)字補x (填充右邊, 寬度為4) |
| 10 | {:x^4} | x10x | 數(shù)字補x (填充兩邊,優(yōu)先左邊, 寬度為4) |
| 13 | {:10} | 13 | 右對齊 (默認, 寬度為10) |
| 13 | {:<10} | 13 | 左對齊 (寬度為10) |
| 13 | {:^10} | 13 | 中間對齊 (寬度為10) |
b、d、o、x分別是二進制、十進制、八進制、十六進制。
其它用法
1.轉(zhuǎn)義“{}”
print('{{hello}} {{{0}}}'.format('world')) #輸出結(jié)果:{hello} {world}跟%中%%轉(zhuǎn)義%一樣,format中用 { 來轉(zhuǎn)義{ ,用 } 來轉(zhuǎn)義 }
2.format作為函數(shù)變量
name = 'InX' hello = 'hello,{} welcome to python world!!!'.format #定義一個問候函數(shù) hello(name) #輸入結(jié)果:hello,inx welcome to python world!!!3.格式化datetime
from datetime import datetime now=datetime.now() print '{:%Y-%m-%d %X}'.format(now) # 輸出結(jié)果:2017-07-24 16:51:424.{}內(nèi)嵌{}
print('hello {0:>{1}} '.format('world',10)) ##輸出結(jié)果:hello world從最內(nèi)層的{}開始格式化
5.嘆號的用法
print(‘{!s}國’.format(‘中’)) #輸出結(jié)果:中國
print(‘{!a}國’.format(‘中’)) #輸出結(jié)果:’\u4e2d’國
print(‘{!s}國’.format(‘中’)) #輸出結(jié)果:’中’國
!后面可以加s r a 分別對應str() repr() ascii() 作用是在填充前先用對應的函數(shù)來處理參數(shù)
差別就是repr帶有引號,str()是面向用戶的,目的是可讀性,repr()是面向Python解析器的,返回值表示在python內(nèi)部的含義,ascii (),返回ascii編碼
更多參考:https://docs.python.org/3/library/string.html#grammar-token-conversion
總結(jié)
以上是生活随笔為你收集整理的python format 用法详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux c】sipc
- 下一篇: linux常用网站(不定期更新)