Using %02d instead of %02s


#1

Why do we use %02d instead of %02s for the date? Does the s stand for string and d for date?

You get the same result when run in the program.


Why did the video use %s over %d
#2

The d stands for digit, and is the same as i for the most part.

%02d

The 02 applies to output size, I believe.

%s

is str type.

All output is string but the symbols indicate the type of content being inserted.

print ("%d" % 42)          # 42

print (type("%d" % 42))    # <class 'str'>

#3

decimal as opposed to octal or hexadecimal or binary


#4

Thinking ‘decimal’ and writing ‘digit’, d’oh!


#5

would %02i give me the same result? I have not learned what %i would do.


#6

Yes, %i will behave the same as %d.

>>> print ("%02d" % 4)
04
>>> print ("%02i" % 4)
04
>>>

Note how the 02 pads the lead with a zero when the digit is singular.


#7

Formatted Output


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.