Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python number format muddle

Please put me out of my misery. The following example seems to be using y twice rather than y and then m.

>>> m=8
>>> y=11
>>> print '20{0:02d}{0:02d}01'.format(y, m)
20111101

I've been through as much of the documentation as I can but can't figure out what would have been relatively straightforward with the old procedure:

>>> print '20%02d%02d01' % ( y, m )
20110801

If someone could explain where I'm going wrong I'd be really grateful.

like image 212
Component 10 Avatar asked Feb 19 '26 00:02

Component 10


1 Answers

This will do what you want:

print '20{0:02d}{1:02d}01'.format(y, m)

You referred to the first argument twice.

like image 133
Levon Avatar answered Feb 21 '26 12:02

Levon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!