24
\$\begingroup\$

I have this at the moment, wondering if there's a cleaner way to do it or if this is already as compact as it gets.

 h = "%s" % str(h) if h >= 10 else "0%s" % str(h)
 m = "%s" % str(m) if m >= 10 else "0%s" % str(m)
 s = "%s" % str(s) if s >= 10 else "0%s" % str(s)
 print("{}:{}:{}".format(h, m, s))
\$\endgroup\$
  • 7
    \$\begingroup\$ Nicest way to pad zeroes to string may be of interest to you. But you might also consider using the datetime.time class, which has some nice string conversion options. \$\endgroup\$ – Kevin Jun 18 '18 at 19:04
  • 2
    \$\begingroup\$ I'm actually using datetime to get these times, didn't occur to me it would have way to convert them into strings. Thanks! \$\endgroup\$ – DavidPH Jun 18 '18 at 19:10
  • 2
    \$\begingroup\$ You mention you are using datetime to get these times, but how exactly? Python's datetime has quite a lot of format codes such as %M for Minute as a zero-padded decimal number. \$\endgroup\$ – fedorqui Jun 19 '18 at 9:55
43
\$\begingroup\$

The % formatting operator already supports 0-padding, just specify the width as 02. Also, you can pass a tuple to combine multiple numbers into the same string with a single format: "%02d:%02d:%02d" % (h, m, s).

In Python 3 the format string method is slightly preferred to the % operator, with Python 3.6 further allowing the format string to reference the variables directly. Using such formatted string literals or f-strings, the formatting would be expressed as: f"{h:02}:{m:02}:{s:02}".

As pointed out in the comments, if you're starting with datetime objects to begin with, you can use their own formatting functionality instead.

|improve this answer|||||
\$\endgroup\$
20
\$\begingroup\$

Using format method. Works in all current Python versions.

'{:02d}:{:02d}:{:02d}'.format(h, m, s)

See more about string formatting here.

|improve this answer|||||
\$\endgroup\$
7
\$\begingroup\$

To summarize the other answers, here are the different ways to solve the problem.

From datetime.datetime or datetime.time object:

t = datetime.time(23, 59, 10)  # Example

(You can also choose 12-hour notation for the hour; see the documentation.)

From 3 individual integers:

h = 23  # Example
m = 59  # Example
s = 10  # Example
  • Very manual formatting:

    h = ("0" if h < 10 else "") + str(h)
    m = ("0" if m < 10 else "") + str(m)
    s = ("0" if s < 10 else "") + str(s)
    print( h + ":" + m + ":" + s )
    
  • Manual formatting with str.zfill():

    h = str(h).zfill(2)
    m = str(m).zfill(2)
    s = str(s).zfill(2)
    print( h + ":" + m + ":" + s )
    
  • Use the % formatting operator:

    print( "%02d:%02d:%02d" % (h, m, s) )
    
  • Use the str.format() method:

    print( "{:02}:{:02}:{:02}".format(h, m, s) )
    
  • Use formatted string literals (Python 3.6+):

    print( f"{h:02}:{m:02}:{s:02}" )
    
|improve this answer|||||
\$\endgroup\$
5
\$\begingroup\$

As mentioned in the comments above, using the datetime module provides all of this functionality:

from datetime import time
h = 21
m = 6
s = 32
output_string = time(h, m, s).strftime("%H:%M:%S")

Hit http://strftime.org/ if you're looking for the complete list of supported string formats.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.