[Tutor] Iterating through a list of strings

python at bdurham.com python at bdurham.com
Mon May 3 19:19:23 CEST 2010


> Agreed that
>
>     line = line[:line.index('%')]
>
> is slightly more readable than
>
>    line = line.split('%', 1)[0]

How about:

line = line.partition('%')[0]

partition() works even if '%' isn't present.

The index() and split() techniques raise exceptions if '%' isn't
present.

Malcolm


More information about the Tutor mailing list