Beginner String formatting question

JAMoore84 at gmail.com JAMoore84 at gmail.com
Sat Jan 26 12:02:17 EST 2008


Hi all,

I am trying to write a simple program that will accept an integral
"time" input in the HHMMSS format and output a "HH:MM:SS" form.  My
code is as follows:
========================
import string

def FormatTime(time):
  '''Converts an HHMMSS string to HH:MM:SS format.'''

  timeString = str(time)  #converts the num to string

  hours    = [timeString[0], timeString[1]]
  minutes = [timeString[2], timeString[3]]
  seconds = [timeString[4], timeString[5]]

  Ftime = "%s:%s:%s",(hours,minutes,seconds)
  clock = Ftime.join()

  return clock
  ===========================

when I run it from IDLE, I get this:

>>> Format.FormatTime(time)
['1', '1', ':', '2', '2', ':', '3', '3']
['1', '1', ':', '2', '2', ':', '3', '3']

My questions-
1) Why is this function printing out twice?
2)It may be a formatting issue, but I want to have the output as
"HH:MM:SS", rather than having it broken out into each cell.  I
thought the 'join' command would do this, but I must not be using it
properly/understanding something.
3)as a side note, I've noticed that the parameter "time" passed in
must be passed in as a string, otherwise I receive an error that
"time" is unsubscriptable.  Am I unable to pass in an int and then
convert it to a string within the function with str()?

I've been at this for a while, so I may not be able to see the forest
through the trees at this point.  I'd greatly appreciate any
suggestions or instruction on these mistakes.

Best,

Jimmy



More information about the Python-list mailing list