Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Pythonで動的な変数の値に0埋め

More than 1 year has passed since last update.

日付関連の取り回しで定期的に使うことになるので、メモ
ドキュメントはこれ
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
Kis3_
全部中途半端系エンジニア インデントは半角4つ派 Javascript/bootstrap4/Java/Swift/Firebase/etc...
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away