+ 1

Python | Time matches

Hey! I want to print all the possible time matches in HH:MM format: 00:00 00:01 00:02 NN:NN 12:58 12:59 Help me, please!

3rd Jan 2021, 5:09 PM
JSiegerheim
8 Réponses
+ 2
Ah, thanks to Lisa I think I know now what you try to do :-D this should work (with Python 3.6+ - otherwise use zfill() from the first answer to achieve the 2-digit numbering): for i in range(12): for j in range (60): print(f"{i:02d}:{j:02d}")
3rd Jan 2021, 5:28 PM
Martin Ed
Martin Ed - avatar
+ 3
f-Strings is a newer way of string formatting in python i have written some string formatting demo some time ago: https://code.sololearn.com/cgUSSCEyco53/?ref=app here you can find a very detailed description of string formatting possibilities: https://realpython.com/python-f-strings/
3rd Jan 2021, 6:10 PM
Martin Ed
Martin Ed - avatar
+ 2
You could use a nested loop. • The outer loop iterates through the hours [0; 12] • The inner loop iterates for each hour through the minutes [0; 59]
3rd Jan 2021, 5:24 PM
Lisa
Lisa - avatar
+ 1
I'm not sure if I got the question, but what works for example: hour = 3 minute = 5 print(str(hour).zfill(2) + ":" + str(minute).zfill(2)) outputs: 03:05
3rd Jan 2021, 5:21 PM
Martin Ed
Martin Ed - avatar
+ 1
Martin Ed Yes, that's what I had in mind 😀 Nice formatting! 😀
3rd Jan 2021, 5:36 PM
Lisa
Lisa - avatar
+ 1
Martin Ed, that worked fine - just had to change 12 to 13 to get all the possible matches up to 12:59 (for i in range(13))! Thanks a lot! but, please, would you explain to me the print parameters you added: f {i:02d}
3rd Jan 2021, 6:04 PM
JSiegerheim
+ 1
i is the variable name 02d formats, for example i = 3 to 03 in the f-string
3rd Jan 2021, 6:07 PM
Lisa
Lisa - avatar
+ 1
Gotcha! Thanks a lot, guys!
3rd Jan 2021, 6:21 PM
JSiegerheim