Beginner String formatting question
Mark Tolonen
mark.e.tolonen at mailinator.com
Sat Jan 26 12:34:27 EST 2008
<JAMoore84 at gmail.com> wrote in message
news:68769e37-7787-414f-abd3-a447341e9f7d at v17g2000hsa.googlegroups.com...
> 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
Your code as displayed above doesn't work.
>>> Format.FormatTime(112233)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "Format.py", line 13, in FormatTime
clock = Ftime.join()
AttributeError: 'tuple' object has no attribute 'join'
>>> Format.FormatTime('112233')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "Format.py", line 13, in FormatTime
clock = Ftime.join()
AttributeError: 'tuple' object has no attribute 'join'
Make sure to copy/paste the exact working code.
Look at the 'time' and 'datetime' modules and the functions and methods
'strftime' and 'strptime' for displaying and parsing times.
--Mark
More information about the Python-list
mailing list