문자열 0 패딩
>>> a = 1
>>> f"{a:01}"
'1'
>>> f"{a:02}"
'01'
>>> f"{a:03}"
'001'
>>> f"{a:10}"
' 1'
>>> f"{a:010}"
'0000000001'
f-string 에서 소수점 자리수 표현
>>> a = 2.341
>>> f"{a:.1f}"
'2.3'
>>> f"{a:.2f}"
'2.34'
>>> f"{a:.3f}"
'2.341'
시간 문자열 조작
def decode_time(data):
h = data // (3600 * 1000)
m = (data - h * (3600 * 1000)) // (60 * 1000)
s = (data - h * (3600 * 1000) - m * (60 * 1000)) // 1000
ms = (data - h * (3600 * 1000) - m * (60 * 1000) - s * 1000)
return f"{h:02}:{m:02}:{s:02}.{ms:03}"
def encode_time(data):
h, m, s, ms = data[:2], data[3:5], data[6:8], data[9:]
return int(ms) + (int(s) + int(m) * 60 + int(h) * 3600) * 1000
a = "01:00:04.001"
print(a)
print(encode_time(a))
print(decode_time(encode_time(a)))