[Tutor] beginsWith multiple prefixes

Bill Campbell bill at celestial.net
Thu Dec 25 06:51:12 CET 2008


On Wed, Dec 24, 2008, bob gailer wrote:
> Emad Nawfal (???? ????) 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:
...
> Above does a lot more work than necessary. Try:
>
> def beginsWith(word, listname):
>    for prefix in listname:
>       if word.startswith(prefix):
>          return True

It might be more useful to return the prefix that matched as the
caller already knows what ``word'' is.

Bill
-- 
INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186

Basic Definitions of Science:
    If it's green or wiggles, it's biology.
    If it stinks, it's chemistry.
    If it doesn't work, it's physics.


More information about the Tutor mailing list