Python: startswith any alpha character
If you want to match non-ASCII letters as well, you can use str.isalpha
:
if line and line[0].isalpha():
You can pass a tuple to startswiths()
(in Python 2.5+) to match any of its elements:
import stringALPHA = string.ascii_lettersif line.startswith(tuple(ALPHA)): pass
Of course, for this simple case, a regex test or the in
operator would be more readable.
An easy solution would be to use the python regex module:
import reif re.match("^[a-zA-Z]+.*", line): Do Something