序文
- 機能をフォーマットするための文字列形式
使い方
- {}による操作の記号表現の基本的な構文
- 各シーケンスは、{}に設定することができます
- フォーマットパラメータの順にそれぞれ対応します、
- 無添字{}、0デフォルトの重量増分場合。
場所によって
print('{0} {1}'.format('hello','world')) # 通过位置
print('{1} {0}'.format('hello','world'))
キーワードで
print('{人物}今天{事件}'.format(人物='我',事件='写博客'))#通过关键字
job = {'person':'你','affair':'写博客'}
# 可用字典当关键字传入值时,在字典前加**即可
print('{person}昨天{affair}'.format(**job))
充填整列
^ < >意味が中央に、背中の幅を右揃え、左揃え
print('{:^18}'.format('hello'))
print('{:>18}'.format('hello'))
print('{:<18}'.format('hello'))
print('{:*<18}'.format('hello'))
print('{:&>18}'.format('hello'))
精度とタイプF
print('{:.1f}'.format(3.1415926))
print('{:.3f}'.format(3.1))
小数変換
シンボル | 説明 |
---|---|
B | バイナリ |
インクルード | オクタル |
D | 小数 |
バツ | 六角 |
print('{:b}'.format(777))
print('{:o}'.format(777))
print('{:d}'.format(777))
print('{:x}'.format(777))
何千人もの区切り
- 通常、お金として使用
print('{:,}'.format(100000000))
print('{:,}'.format(12345.6789))