[Tutor] beginsWith multiple prefixes

Kent Johnson kent37 at tds.net
Thu Dec 25 14:17:25 CET 2008


On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (عماد نوفل)
<emadnawfal at gmail.com> wrote:
> Hi Tutors,
> I want a function that acts like the startswith method, but can take
> multiple prefixes. As an amateur programmer, I came up with this one, and it
> works fine, but my experience tells me that my solutions are not always the
> best ones around. Can you please tell me what a better option might be:

Since Python 2.5, startswith() accepts a tuple as the match parameter,
so you can write
def beginsWith(word, listname):
  return word.startswith(tuple(listname))

or just us startswith() directly.

Kent


More information about the Tutor mailing list