1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python format関数で数値の書式指定メモ

Last updated at Posted at 2020-08-21

pythonのformat関数のよく使う書式メモ。

>>> # 引数の数だけ{}を書く
>>> print("{}{}".format(12, 34))
1234

>>> # 引数のインデックスを指定
>>> print("{1}:{0}".format(12, 34))
34:12

>>> # 桁数指定(空白で埋まる)
>>> print("{1:4}:{0:3}".format(12, 34))
  34: 12

>>> # 千の位にカンマ
>>> print("{:,}".format(123456789))
123,456,789

>>> # 桁数指定して右寄せ
>>> print("→{:>8}←".format(12))
      12

>>> # 桁数指定して左寄せ
>>> print("→{:<8}←".format(12))
12      

>>> # 小数点桁数指定
>>> print("{:>.4f}".format(12.456))
12.4560

>>> # 全体の桁数と小数点以下の桁数指定
>>> print("→{:>8.4f}←".format(12.456))
 12.4560

>>> # 桁数の埋める文字指定
>>> print("→{:!>8}←".format(12))
→!!!!!!12
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?