Zero padding for dynamic variable values in Python

Note that it will be used regularly for date-related handling. Document is this OK with str.zfill (width) Don't forget that it becomes the string str.

Sample used

When month and day are not 2 digits, fill the missing digits with 0 and output so that it becomes 2 digits

year = 2020
month = 1
day = 1
while day <= 31:
 Make sure #month and day are 2 digits
  yyyymmdd = str(year) + "/" + str(month).zfill(2) + "/" + str(day).zfill(2)
  print(yyyymmdd)
  day += 1

Output result

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

Recommended Posts

Zero padding for dynamic variable values in Python
Swapping values in Python
Zero padding in Golang
Search for strings in Python
Techniques for sorting in Python
Transfer parameter values in Python
About "for _ in range ():" in python
Check for memory leaks in Python
Check for external commands in python
Run unittests in Python (for beginners)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Template for writing batch scripts in python
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
How to dynamically zero pad in Python
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Learn dynamic programming in Python (A ~ E)
Template for creating command line applications in Python
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
++ and-cannot be used for increment / decrement in python
Ant book in python: Sec. 2-3, Dynamic Programming (DP)
How to embed a variable in a python string
Import-linter was useful for layered architecture in Python
I investigated in detail about variable processing in python
Add quotation marks ">" for replying emails in Python3
Logging settings for daily log rotation in python
Tips for hitting the ATND API in Python
Refined search for Pokemon race values using Python
Try to calculate RPN in Python (for beginners)
Testing methods that return random values in Python
Notes for implementing simple collaborative filtering in Python
Don't use readlines () in your Python for statement!
Enter a specific value for variable in tensorflow
[Implementation for learning] Implement Stratified Sampling in Python (1)
Settings for Python coding in Visual Studio Code
Hit the Firebase Dynamic Links API in Python
[Introduction for beginners] Working with MySQL in Python
Basic story of inheritance in Python (for beginners)
Settings for getting started with MongoDB in python
2016-10-30 else for Python3> for:
Python underscore variable
Quadtree in Python --2
Python in optimization
python [for myself]
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Notes on exchanging and multiple assignment of Python variable values ​​learned in quiz format
Python: Tips-Swap values
Meta-analysis in Python