1

I am new in python. In the following line '{0:6}{1:02d}'.format(date, hour)

I figured out that hour should be 0 filled to two digits. But I can't figure out what it is formatting date to be. afaik, both date and hour are int values here.

1
  • 2
    an example would be better. Commented Apr 16, 2015 at 3:22

2 Answers 2

3

{0:6} is just going to take the 0th argument (date) and print it with a minimum of 6 characters. It can be an integer, a string, ... Nothing else special there. It is a guess at what format was intended for date (ie. May1st, 5/1, ...)

You are correct about the interpretation of the {1:02d} which is the hour field print in a minimum of 2 decimal digits with 0's to pad.

Sign up to request clarification or add additional context in comments.

Comments

2

Try it and see:

a = 10
b = 6
print('{0:6}{1:02}'.format(a, b))
#output => '    1006'

So like 02 adds "0"s to the variable until it is of 2 length. Just adding a number (6) will add spaces to the front until the variable length is 6.

Python's Common String Operator's Docs has more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.