詳細なフォーマット機能Python--

序文

  • 機能をフォーマットするための文字列形式

使い方

  • {}による操作の記号表現の基本的な構文
  • 各シーケンスは、{}に設定することができます
  • フォーマットパラメータの順にそれぞれ対応します、
  • 無添字{}、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))

ここに画像を挿入説明

公開された29元の記事 ウォン称賛19 ビュー1308

おすすめ

転載: blog.csdn.net/s1156605343/article/details/104846523