如何将一个int的固定个数的字符转换成一个字符串?

How to get a fixed number of characters of an int into a string?

我正在尝试从 3 个 int 变量格式化表示单圈时间的字符串:min、sec、ms

我试过这个:

    full_time = f'{min}:{sec}:{ms}'

但我想始终使用 2 个字符表示秒,使用 3 个字符表示毫秒(例如:1:09:077) 我见过类似这样的东西:{:_<2} 但我不知道该写在哪里。 或者您推荐另一种格式化字符串的方法吗?

感谢您的回答!

语法是:

f'{mm}:{ss:02}:{ms:03}'

其他例子,理解语法

# zeros padding
print(f'{mm}:{ss:02}:{ms:03}')  # 5:03:002

# space padding
print(f'{mm}:{ss:02}:{ms:3}')  # 5:03:  2

# zeros padding and decimals : total 6 len, including dot and 2 decimals
print(f'{mm}:{ss:02}:{ms:06.2f}')  # 5:03:002.00

# zeros leading
print(f'{mm}:{ss:02}:{ms:<03}')  # 5:03:200