This is probably faster: def alfa_(w): return w.replace("_", "a").isalpha() This is another solution, but it's probably slower, you can time it: from string import letters _setalpha = set(letters + "_") def alfa_2(w): return not (set(w) - _setalpha) Bye, bearophile