How Can I Improve My Print Statement in Python?

March 19, 2026 5 answers
Asked by CoolCoder93
Hi everyone! I'm just starting out with Python and I'm having a bit of trouble with a homework task that involves calculating some running times. The task goes like this: If I leave my house at 6:52 AM and run 1 kilometer at an easy pace of 8:15 per kilometer, followed by 3 kilometers at a tempo of 7:12 per kilometer, and finish with another 1 kilometer at an easy pace, what time will I get home for breakfast? I've managed to compute the hours, minutes, and seconds correctly, but I'm not happy with how the output looks. Here's my code where I'm currently using the print function: ```python print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":",seconds_conv2) ``` The printed result is "Breakfast start time would be: 7 : 30 : 6", but I want to improve the formatting. Any suggestions on how to adjust this? Here's the full code I'm working with: # Starting Conversion start = 6*3600 + 52*60 start_conv = 0 # Formula: start += (8*60 + 15) start += (3*(7*60 + 12)) start += (8*60 + 15) # Hours: hours_conv = start // 3600 # Minutes: minutes_conv1 = start % 3600 minutes_conv2 = minutes_conv1 // 60 # Seconds: seconds_conv1 = start % 3600 seconds_conv2 = seconds_conv1 % 60 # Final Results: print(hours_conv) print(minutes_conv2) print(seconds_conv2) # Final Results: print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":",seconds_conv2)

Answers

DataNinja42
If you look at the documentation for the `print` function, you'll see it uses a space as the default separator. You can change that with the `sep` argument. Try doing this: ```python print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":", seconds_conv2, sep="") ``` Alternatively, you could combine your strings into one and print it out, like this: ```python message = f"Breakfast start time would be {hours_conv}:{minutes_conv2}:{seconds_conv2}" print(message) ```

HelpSeeker45: Thanks for pointing that out! I did check the docs but didnโ€™t realize the `sep` option. I tried using an f-string afterwards and it worked great!

Reply to this answer
ThirtyMinuteChef
Why not utilize the `datetime` module for simpler handling? Hereโ€™s a quick example: ```python from datetime import datetime, timedelta start = datetime.strptime("06:52", "%H:%M") end = start + timedelta(seconds=60*60 + 15) answer = end.strftime("%H:%M:%S") print(f"Breakfast start time would be {answer}") ```
Reply to this answer
CodeExplorer57
It looks like your calculation logic is solid! The issue really comes down to how you format the output. Just using f-strings or adjusting the `sep` should sort that out!

FeedbackHunter: Thanks for the encouragement! I struggled to find practical advice on other forums, so I really appreciate it. My formatting is fixed now!

Reply to this answer
PrintMaster99
Youโ€™ve got a couple of options! By default, `print()` separates different arguments with a space, which is why youโ€™re seeing those gaps. You can use: ```python print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":", seconds_conv2, sep="") ``` Or just use f-strings directly for better control: ```python print(f"Breakfast start time would be {hours_conv}:{minutes_conv2}:{seconds_conv2}") ```

AppreciativeNewbie: Thanks a lot! Iโ€™m trying it out as we speak!

Reply to this answer
TechieGal88
You might want to check out f-strings if you haven't already! Theyโ€™re a fantastic way to handle string formatting in Python. You could also use `str()` to convert numbers to strings and concatenate them, but f-strings are definitely more elegant!

CuriousRunner21: I appreciate the tip! Iโ€™ll look into f-strings right away and see how I can fix my print output. Thanks a bunch!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions