我需要在 Python3 中使用前导符号和小时(认为时区偏移)来格式化时间:
>>> hour = 2
>>> print("T%02d" % hour)
T02
>>> print("T%+02d" % hour)
T+2
预期结果为T+02
最佳答案
该字段包含 +
字符,需要将其计入宽度。您希望字段的宽度为 3 个字符:
print("T%+03d" % hour)
演示:
>>> hour = 2
>>> print("T%+03d" % hour)
T+02
关于python - 如何格式化用符号和零填充的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46738255/