On 22/06/2013 22:44, Albert-Jan Roskam wrote:
--- Original Message -----
From: Jim Byrnes <jf_byr...@comcast.net>
To: tutor@python.org
Cc:
Sent: Saturday, June 22, 2013 11:01 PM
Subject: [Tutor] How convert an int to a string
I need to convert a series of digits like 060713 to a string so I can
make it look like a date 06-07-13.
a = 060713
a[:2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'
b = str(a)
b[:2]
'25'
b
'25035'
I was confused at first but then realized that the 0 makes it octal. I
thought str() would do it but it didn't. Reading about str() it talks of
string representation. So how can I convert it to a true string I can
slice and build my date look a like?
weird indeed. Can wait to hear more about it. But oct() does the trick.
a = 060713
a
25035
import time
time.strftime("%d-%m-%y", time.strptime(oct(a), "%d%m%y"))
'06-07-13'
b = str(oct(a))
"%02s-%02s-%02s" % (b[:2], b[2:4], b[4:])
'06-07-13'
How do you propose handling the errors that will occur when any of day,
month or year have a non octal digit?
--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.
Mark Lawrence
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor