ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Splitting a number into even- and odd- numbered digits
by John Fouhy other posts by this author
Apr 19 2006 5:49PM messages near this date
Re: [Tutor] Splitting a number into even- and odd- numbered digits | [Tutor] Reqiest Centre - Adding New Call
On 20/04/06, Carroll, Barry <Barry.Carroll@[...].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.
_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Barry Carroll
Python
Barry Carroll
Barry Carroll
John Fouhy

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved