[Tutor] Splitting a number into even- and odd- numbered digits
John Fouhy
john at fouhy.net
Thu Apr 20 02:48:34 CEST 2006
On 20/04/06, Carroll, Barry <Barry.Carroll at psc.com> wrote:
> The following code fragment does the job but seems sort of brutish and inelegant to me:
>
> >>>>>>>
> >>> s = '987654321'
> >>> odd = ''
> >>> for c in s[::-2]:
> ... odd = c + odd
> ...
> >>>
String slicing will actually produce strings :-)
eg:
>>> s = '987654321'
>>> s1, s2 = s[::-2], s[-2::-2]
>>> s1
'13579'
>>> s2
'2468'
--
John.
More information about the Tutor
mailing list