Datetime format


I am not able understand that what are those ‘%02d’ and %04d’ instead we can also use '%s
please tell!

Here is another one

Is we always need to assign a variable to get “month,year,days” and other things?


%d is a placeholder for decimals values (d of decimals what a coincidence) while %s is a string placeholder (s of string, that can’t be a coincidence anymore)

its explained in the lesson:

%02d pads the month and day numbers with zeros to two places, and %04d pads the year to four places. This is one way dates are commonly displayed.

if we just did:

print '%d-%d-%d' % (now.month,, now.year)

then we would get:


while with %02d we get 03 for march instead of 3 with %d. It pads any shortage with zeroes, i think its quite neat.


Ok got it! Thanks :smile: