[Tutor] Splitting a string

col speed ajarncolin at gmail.com
Tue Feb 8 10:36:09 CET 2011


On 8 February 2011 16:20, tee chwee liong <tcl76 at hotmail.com> wrote:

>  hi all,
>
> >i have a function which returns a string. for eg: X='101110'. i want to
> search for 0 and highlight the location.
> i> am thinking of defining X as a list. but how can i split 101110 as there
> are no spaces in between? i'm thinking if i can put it into a list:
> >X=['1','0','1','1','1','0'], then i can use index to point out the
> locations of the 0. pls advise.
> i>'m using Python2.5 and WinXP.
>
> >thanks
> >tcl
>
> You can use x.find("0"), but this will only give you the first position.
How about our friend 'enumerate'?:
for index, i in enumerate(x):
    if i == "0":
        print index

Cheers


--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110208/a90e3f0c/attachment.html>


More information about the Tutor mailing list