[Tutor] Splitting a string

Alan Gauld alan.gauld at btinternet.com
Tue Feb 8 19:23:11 CET 2011


"tee chwee liong" <tcl76 at hotmail.com> wrote

> i have a function which returns a string. for eg: X='101110'. 
> i want to search for 0 and highlight the location. 

I'm not sure what you mean by highlight the location. 
Is it a GUI? Are you colour coding the characters?

> i am thinking of defining X as a list. 

You shouldn't need to, you can treat the string as a 
sequence directly

indexes = [i for i, c in enumerate(X) if char == '0']

Will give you the list of indexes you need. 
How you use that to highlight the zeros will depend 
on your UI I guess.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list