[Tutor] How convert an int to a string
David Rock
david at graniteweb.com
Sun Jun 23 00:10:42 CEST 2013
* Jim Byrnes <jf_byrnes at comcast.net> [2013-06-22 16:01]:
> 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?
Is there a requirement to store them as numbers in the first place? Why
not just store them as a string?
a = '060713'
--
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/tutor/attachments/20130622/e32b5570/attachment.pgp>
More information about the Tutor
mailing list