2

I need to format time with leading sign and hour (think timezone offset) in Python3:

>>> hour = 2
>>> print("T%02d" % hour)
T02
>>> print("T%+02d" % hour)
T+2

Expected result is T+02

0

1 Answer 1

5

The field includes the + character, which needs to be accounted for in the width. You want your field to be 3 characters wide:

print("T%+03d" % hour)

Demo:

>>> hour = 2
>>> print("T%+03d" % hour)
T+02
Sign up to request clarification or add additional context in comments.

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.