0
1

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で動的な変数の値に0埋め

Last updated at Posted at 2020-02-20

日付関連の取り回しで定期的に使うことになるので、メモ
ドキュメントはこれ
str.zfill(width)でOK
文字列strになるのだけお忘れなく。
#使用サンプル
monthdayが2桁になっていない場合足りない桁は0で埋めて2桁になる様に出力

year = 2020
month = 1
day = 1
while day <= 31:
  #monthとdayは必ず2桁になるように
  yyyymmdd = str(year) + "/" + str(month).zfill(2) + "/" + str(day).zfill(2)
  print(yyyymmdd)
  day += 1

#出力結果

2020/01/01
2020/01/02
2020/01/03
2020/01/04
2020/01/05
2020/01/06
2020/01/07
2020/01/08
2020/01/09
2020/01/10
2020/01/11
2020/01/12
2020/01/13
2020/01/14
2020/01/15
2020/01/16
2020/01/17
2020/01/18
2020/01/19
2020/01/20
2020/01/21
2020/01/22
2020/01/23
2020/01/24
2020/01/25
2020/01/26
2020/01/27
2020/01/28
2020/01/29
2020/01/30
2020/01/31
0
1
1

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?