[Tutor] searching for multiple strings in line.starswith()

Alan Gauld alan.gauld at btinternet.com
Mon Jul 19 19:55:30 CEST 2010

"Bala subramanian" <bala.biophysics at gmail.com> wrote

> I have to extract the line from a file that does not contain a set 
> of
> strings in the start of the line, i wrote the following code.
> for index, line in enumerate(myvar.split('\n')):
>    if line.startswith('') not in ['#Cluster','#Centroid','#End']:

line.startswith() returns a boolean result - True or False
Neither of these is in your list so the if test always passes.

You need to apply startwith to each item in your list.
You could use a list comprehension/generator expression
- or find another approach, maybe using slicing?


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list