An isalpha() that accepts underscores as well

Zajcev Evgeny zevlg at yandex.ru
Sun Feb 26 14:26:59 EST 2006


egbert <egbert.bouwman at hccnet.nl> writes:

> The string method isalpha() returns True when all characters in the
> string are alphabetic. Unfortunately the underscore is not alphabetic.
> A function that does what I need is:
>
> def alfa_(w):
>     return "".join(w.split("_")).isalpha()
>
> but for the kind of strings that I have this is about ten times
> slower than isalpha() sec. Any suggestions ?
> Thanks.

what about

    def alfa_(w):
        return w.isalpha() or w.find('_') != -1

? but yes it does scan `w' twice ..

You could also do something like:

    def alfa_(w):
        for c in w:
            if not c.isalpha() and not c == '_':
               return False
        return True

-- 
lg



More information about the Python-list mailing list