-1

I'm trying to format a string that represents a lap-time from 3 int variable: min, sec, ms

I tried this:

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

but I want to always have 2 characters for the seconds and 3 characters for the milliseconds (ex: 1:09:077) I've seen something that looks like this: {:_<2} but i don't know where to write it. Or do you recomend another way of formatting strings?

Thanks for your answers!

  • 1
    https://docs.python.org/3.4/library/string.html#string-formatting (Ed Ward has the specific string for you already) Although you should consider datetime, which has formatting functions, as well as actual time operations. – Kenny Ostrom Mar 17 '20 at 18:25
  • 1
    `f"{min}:{sec:02}:{ms:03}"`... https://stackoverflow.com/questions/339007/how-to-pad-zeroes-to-a-string – Ed Ward Mar 17 '20 at 18:26
  • If you search in your browser for "Python format string", perhaps with a modifier of "time", you will get answers far more complete. As the intro tour tells you, we expect you to do this research before posting a question here. – Prune Mar 17 '20 at 18:48

1 Answers1

0

The syntax is :

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

Other examples, to understand the syntax

# 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
azro
  • 53,056
  • 7
  • 34
  • 70